当前位置: 首页 > article >正文

vue3:初学 vue-router 路由配置

承上一篇:nodejs:express + js-mdict 作为后端,vue 3 + vite 作为前端,在线查询英汉词典

安装 cnpm install vue-router -S

现在讲一讲 vue3:vue-router 路由配置

cd \js\mydict-web\src
mkdir router
cd router 
我还没有编写过 component,先拿 HelloWorld.vue 练练手。编写 router/index.js  如下

import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
    {   path:'/',
        name:'hello',
        component:() => import('../components/HelloWorld.vue')
    }
]

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

export default router

cd  \js\mydict-web\src
copy main.js main1.js
修改 main1.js  如下

import { createApp } from 'vue'
import App from './App1.vue'
import router from './router'

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

copy App.vue App1.vue 
修改 App1.vue  如下,增加一行 <router-view></router-view> 后面代码不变

<template>
  <div id="app">
    <input v-model="sWord" placeholder="请输入英文单词" @keyup.enter="search">
    &nbsp; <button @click="search">查询</button>
    &nbsp; <button @click="prefix">前缀查询</button>
    &nbsp; <button @click="fuzzy">模糊查询</button>
    <div v-if="result">
      <h3>查询结果</h3>
      <div id="result1" ref="result1" v-html="result"></div>
    </div>
    <div v-if="error">{{ error }}</div>
    <router-view></router-view>
  </div>
</template>

cd  \js\mydict-web\
copy index.html  index1.htm
修改 index1.htm  如下

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite 在线英汉词典查询</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main1.js"></script>
  </body>
</html>

运行 cmd
cd \js\mydict-app
node server/app.js
Server is running on port:8006

运行 cmd
cd \js\mydict-web
npm run dev 

访问 http://localhost:5173/index1.htm 


http://www.kler.cn/a/571834.html

相关文章:

  • 删除pytorch
  • 贪心算法--给定一个只包含X和.字符串
  • AI-Ollama本地大语言模型运行框架与Ollama javascript接入
  • Centos修改ip
  • Pytorch实现之促进恶意软件图像合成GAN
  • Matlab——添加坐标轴虚线网格的方法
  • 某系统webpack接口泄露引发的一系列漏洞
  • Python 深度学习入门:TensorFlow 与 PyTorch,神经网络模型构建实战
  • 决策树、朴素贝叶斯、随机森林、支持向量机、XGBoost 和 LightGBM算法的R语言实现
  • Leetcode 面试150题(三)
  • FastGPT 引申:Rerank 函数调用实例
  • 小白入坑向:Java 全栈系统性学习推荐路线之一
  • hive tez使用小文件合并参数后,单个文件大小大于128MB
  • 单片机应用:定时闪烁的LED小灯的实现
  • 非平衡数据的处理
  • DeepSeek本地接口调用(Ollama)
  • 【一文学会 HTML】
  • 基于单片机的可燃气体火灾报警器的设计与实现
  • 2.反向传播机制简述——大模型开发深度学习理论基础
  • 如何将飞书多维表格与DeepSeek R1结合使用:效率提升的完美搭档