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 如下

javascript 复制代码
import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
    {   path:'/',
        name:'hello',
        component:() => import('../components/HelloWorld.vue'),
        props:{ msg:'Hello,Vite' } // 传参数
    }
]

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

export default router

cd \js\mydict-web\src

copy main.js main1.js

修改 main1.js 如下

javascript 复制代码
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> 后面代码不变

html 复制代码
<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 如下

html 复制代码
<!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

相关推荐
共享家95275 小时前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
Halo_tjn6 小时前
基于封装的专项 知识点
java·前端·python·算法
摘星编程7 小时前
OpenHarmony环境下React Native:自定义useTruncate文本截断
javascript·react native·react.js
Duang007_7 小时前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python
有来技术7 小时前
Spring Boot 4 + Vue3 企业级多租户 SaaS:从共享 Schema 架构到商业化套餐设计
java·vue.js·spring boot·后端
东东5168 小时前
学院个人信息管理系统 (springboot+vue)
vue.js·spring boot·后端·个人开发·毕设
2601_949868369 小时前
Flutter for OpenHarmony 电子合同签署App实战 - 主入口实现
开发语言·javascript·flutter
m0_748229999 小时前
Vue2 vs Vue3:核心差异全解析
前端·javascript·vue.js
C澒9 小时前
前端监控系统的最佳实践
前端·安全·运维开发
xiaoxue..9 小时前
React 手写实现的 KeepAlive 组件
前端·javascript·react.js·面试