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

学习Vue-router的使用

创建vue-cli的项目

项目构建成功之后通过运行指令使用项目

在views下创建test.vue页面添加到App.vue通过router的语法

然后通过router文件夹下的index.js配置路由然后在也面点击跳转到路由test页面

要求通过该实验的练习,学习并会使用Vue-router的基本语法,以及路由跳转和路由嵌套

具体操作步骤:

1)通过Vue-cli构建创建项目  

2)通过指令运行Vue-cli脚手架

3)创建test.vue的页面
4)配置路由设置在主页面进行跳转看是否跳转成功

vue-router 是 Vue.js 官方的路由管理器,用于实现单页面应用(SPA)的路由功能。下面为你介绍它的基本使用步骤。

1.项目初始化

如果使用 Vue 3,可在项目根目录下执行以下命令来安装 vue-router

 
npm install vue-router@4

# 创建项目(选择自定义,选择vue3+router)

vue create lx-vue-router

#进入项目目录

cd lx-vue-router

#运行项目

npm run serve

访问链接http://localhost:8087/

#验证是否安装router

npm list vue-router

2.局部配置测试(如果选择了eslient)

 自动修复 ESLint 错误

ESLint 提供了一个 --fix 选项,可以自动修复大部分格式问题。运行以下命令:

npx eslint --fix src/

会自动修复所有可以修复的 ESLint 错误。

 自动修复 Prettier 错误

Prettier 也可以自动格式化代码。运行以下命令:

npx prettier --write src/

3.页面测试(创建了Home.vue与Test.vue)

// router/index.js
import { createRouter, createWebHistory } from 'vue-router'  

import Home from '../views/Home.vue'

import Test from '../views/Test.vue'

const routes = [

  {

    path: '/',

    name: 'Home',

    component: Home,

  },

  {

    path: '/test',

    name: 'Test',

    component: Test,

  },

];

const router = createRouter({  

  history: createWebHistory(process.env.BASE_URL),

  routes,

});

export default router;
//views/Home.vue
<template>

  <div>

    <h1>This is the Home Page</h1>

    <p>Welcome to the Home page!</p>

  </div>

</template>



<script>

export default {

  name: 'Home',

};

</script>



<style scoped>

h1 {

  color: #42b983;

}

</style>
//views/Test.vue
<template>

  <div>

    <h1>This is the Test Page</h1>

    <p>Welcome to the Test page!</p>

  </div>

</template>



<script>

export default {

  name: 'Test',

};

</script>



<style scoped>

h1 {

  color: #42b983;

}

</style>
//app.vue
<template>

  <div id="app">

    <nav>

      <router-link to="/">Home</router-link> |

      <router-link to="/test">Test</router-link>

    </nav>

    <router-view/>

  </div>

</template>

<script>

export default {

  name: 'App',

};

</script>



<style>

#app {

  font-family: Avenir, Helvetica, Arial, sans-serif;

  text-align: center;

  color: #2c3e50;

  margin-top: 60px;

}

</style>
// main.js
import { createApp } from 'vue'

import App from './App.vue'

import router from './router'



createApp(App)

    .use(router)  // 使用 Vue 3 的插件注册方式

.mount('#app')

 

发现可以通过路由实现正确跳转,测试成功,接下来开始配置项目。


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

相关文章:

  • 【Python实用技巧】OS模块详解:文件与目录操作的瑞士军刀
  • ENSP学习day11
  • 探索 Vue 中的多语言切换:<lang-radio /> 组件详解!!!
  • c++面经
  • 2025年渗透测试面试题总结-某快手-安全工程师(题目+回答)
  • Spring Cloud Config 快速介绍与实例
  • 企业级风控系统设计:速卖通API数据+区块链存证防篡改方案
  • 索引定义、作用和分类
  • C++:异常的深度解析
  • 新能源动力电池测试设备深度解析:充放电设备与电池模拟器的差异及技术趋势
  • 如何快速解决django报错:cx_Oracle.DatabaseError: ORA-00942: table or view does not exist
  • 【git】更换账号登录VSCode后报错remote: Permission to **/**.git denied to ***.
  • 在 Mermaid 流程图里“驯服”quot;的魔法指南!!!
  • Go语言实现抖音视频下载器
  • K8S学习之基础五十八:部署nexus服务
  • asp.net mvc 向前端响应json数据。用到jquery
  • postman测试调用WebService时不会自动添加命名空间
  • 【Pandas】pandas Series to_frame
  • HTTP 1.0和2.0 有什么区别?
  • Ubuntu24.04 离线安装 MySQL8.0.41