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')

操作:

首页点击问题即可跳转。

相关推荐
华洛2 分钟前
讲讲如何在传统产品中挖掘AI需求
javascript·产品经理·产品
why技术28 分钟前
AI Coding开始进入第四个时代,我还没上车呢!
前端·人工智能·后端
大家的林语冰1 小时前
CSS 已死?DOM 性能黑洞!Pretext 排版革命让你在文本间跳舞,没有 DOM 也能纵享丝滑~
前端·javascript·css
vipbic1 小时前
我也该升级了,陪伴了我7年的博客
前端
Lee川2 小时前
RAG 实战:从一篇掘金文章出发,拆解检索增强生成的全链路
前端·人工智能·后端
Lee川2 小时前
MCP 高德地图实战:当 AI 学会使用工具,一个协议如何重塑大模型的行动边界
前端·人工智能·后端
ZC跨境爬虫2 小时前
跟着 MDN 学CSS day_14:(尺寸调整技能测试与实战解析)
前端·css·ui·html·tensorflow
kyriewen2 小时前
用魔法打败魔法:我让AI替我去面试前端岗,AI面试官给我打了92分,还发了offer
前端·javascript·面试
IT_陈寒2 小时前
Redis批量删除踩了坑,原来DEL命令不是万能的
前端·人工智能·后端
lichenyang4533 小时前
鸿蒙聊天 Demo 练习 06:AI 思考气泡与 MVVM + Controller 结构重构
前端