vue项目组装-路由-文件修改地方

去找资源

https://download.csdn.net/download/hashiqimiya/92753755https://download.csdn.net/download/hashiqimiya/92753755

配置好资源。

操作:

mkfile src/views/Home.vue

复制代码
<template>
  <div>
    <header>
      <nav>
        <router-link to="/" class="logo">Vue博客</router-link>
        <ul class="nav-links">
          <li><router-link to="/">首页</router-link></li>
          <li><router-link to="/questions">问题</router-link></li>
          <li><a href="#">关于</a></li>
        </ul>
      </nav>
    </header>
    </div>
</template>
<style scoped>
</style>

mkfile src/views/Questions.vue

复制代码
<template>
  <div>
    <header>
      <nav>
        <router-link to="/" class="logo">Vue博客</router-link>
        <ul class="nav-links">
          <li><router-link to="/">首页</router-link></li>
          <li><router-link to="/questions">问题</router-link></li>
          <li><a href="#">关于</a></li>
        </ul>
      </nav>
    </header>
    
    <main>
      <div class="questions-container">
        <div class="questions-header">
          <h1>问题列表</h1>
          <button class="add-question-btn" @click="showAddForm = !showAddForm">
            {{ showAddForm ? '取消' : '新增问题' }}
          </button>
        </div>
        
        <!-- 新增问题表单 -->
        <div v-if="showAddForm" class="add-question-form">
          <input 
            type="text" 
            v-model="newQuestion.title" 
            placeholder="问题标题"
            class="question-input"
          >
          <textarea 
            v-model="newQuestion.content" 
            placeholder="问题内容"
            class="question-textarea"
          ></textarea>
          <button @click="addQuestion" class="submit-btn">提交问题</button>
        </div>
        
        <!-- 问题列表 -->
        <div class="questions-list">
          <div v-for="question in questions" :key="question.id" class="question-card">
            <h3>{{ question.title }}</h3>
            <p>{{ question.content }}</p>
            <div class="question-meta">
              <span>{{ question.date }}</span>
            </div>
          </div>
          <div v-if="questions.length === 0" class="empty-state">
            <p>暂无问题,点击"新增问题"添加</p>
          </div>
        </div>
      </div>
    </main>
    
    <footer>
      <div class="footer-content">
        <p>&copy; 2026 Vue博客. 保留所有权利.</p>
      </div>
    </footer>
  </div>
</template>

<script>
export default {
  name: 'Questions',
  data() {
    return {
      showAddForm: false,
      newQuestion: {
        title: '',
        content: ''
      },
      questions: [
        {
          id: 1,
          title: '如何使用Vue 3的Composition API?',
          content: '我想了解Vue 3的Composition API的基本使用方法,特别是如何组织代码结构。',
          date: '2026-03-22'
        },
        {
          id: 2,
          title: 'TypeScript和Vue 3的结合使用',
          content: '在Vue 3项目中如何更好地使用TypeScript,有什么最佳实践吗?',
          date: '2026-03-21'
        }
      ]
    }
  },
  methods: {
    addQuestion() {
      if (this.newQuestion.title && this.newQuestion.content) {
        const newId = this.questions.length > 0 ? Math.max(...this.questions.map(q => q.id)) + 1 : 1
        this.questions.unshift({
          id: newId,
          title: this.newQuestion.title,
          content: this.newQuestion.content,
          date: new Date().toISOString().split('T')[0]
        })
        // 重置表单
        this.newQuestion.title = ''
        this.newQuestion.content = ''
        this.showAddForm = false
      }
    }
  }
}
</script>

mkfile src/router/index.js

复制代码
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Questions from '../views/Questions.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/questions',
    name: 'Questions',
    component: Questions
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router

App.vue

复制代码
<template>
  <div>
    <router-view></router-view>
  </div>
</template>

main.js

增加

import router from './router'

.use(router)

修改成

复制代码
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'

createApp(App).use(router).mount('#app')

操作:

首页点击问题即可跳转。

相关推荐
Avan_菜菜17 小时前
AI 能写代码了,为什么我反而开始要求它先写文档?
前端·github·ai编程
JieE21220 小时前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE21221 小时前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
爱勇宝21 小时前
鸿蒙生态的下半场:开发者不只要能开发,还要能赚钱
android·前端·程序员
IT_陈寒1 天前
SpringBoot这个自动配置坑我跳了三次
前端·人工智能·后端
kyriewen1 天前
我用 AI 一周写完了整个项目,上线第一天就崩了——这是我踩过最贵的 5 个坑
前端·javascript·ai编程
Larcher1 天前
AI Loop:让AI像人一样自主完成任务的核心机制
javascript·人工智能·设计模式
默_笙1 天前
🃏 JS 只有 8 种数据类型,但我花了 2 天才搞懂 null 和 undefined 的区别
javascript
牧艺1 天前
从零到协同:构建类飞书在线文档系统的五个技术重难点
前端·人工智能
jump_jump1 天前
流式 HTML:从 htmx 片段装配到浏览器原生增量渲染
javascript·性能优化·前端工程化