vue 之vue-router 路由篇

1.npm install vue-router@3.5.0        下载路由

   如果报错,请查看node是否有问题

2..配置路由,在src路径下面新建一个route文件夹,然后新建一个index.js,例如:

//route->index.js内容

import Vue from 'vue'
import VueRouter from 'vue-router'

//引入页面
import Allpages from '../views/allpages.vue'
import Job from '../views/job.vue'
Vue.use(VueRouter);
let router = new VueRouter({
    routes: [
        {
            //路由匹配路径
            path: "/",
            //路由名称
            name:"allpages",
            //定义路径所对应的组件(当你访问当前路径是要展示哪个组件)
            component: Allpages
        },
        {
            path: "/job",
            component: Job
        }// {
        //   path: '/about',
        //   name: 'about',
       //路由懒加载,路由的另一种写法
        // component: () => import(@/views/AboutView.vue)
  // },
    ]
})



// 导出路由
export default router

3.在main.js里面引入注册路由

//main.js里面的内容

//引入vuejs
import Vue from 'vue'
//引入app跟组件
import App from './App.vue'
新增   //引入路由
import router from '../src/route/index.js'
//设置模式
Vue.config.productionTip = false

// 创建vue实例
new Vue({
  //注册路由
  router,
  新增  // 将组件渲染
  render: h => h(App),
}).$mount('#app')
//将vue实例挂载到#app标签上

4.在app.vue显示,用来展示路由 所匹配的组件<router-view></router-view>

<template>
  <div id="app">
    {{ num }}
    <button @click="num++">++</button>
    新增   <router-view></router-view>
    <!-- 使用组件 -->
  </div>
</template>

<script>
//引入组件

export default {
  name: "App",
  data() {
    return {
      num: 0,
    };
  },
  components: {
    //注册组件
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

5.如果报错可能是eslin的问题,把eslin关一下 ,在vue.config.js里面

You may use special comments to disable some warnings.
Use // eslint-disable-next-line to ignore the next line.
Use /* eslint-disable */ to ignore all warnings in a file.
ERROR in [eslint]
D:\AAA重新加油\day6\vuedome1\src\views\allpages.vue
  1:1  error  Component name "allpages" should always be multi-word  vue/multi-word-component-names

D:\AAA重新加油\day6\vuedome1\src\views\job.vue
  1:1  error  Component name "job" should always be multi-word  vue/multi-word-component-names

✖ 2 problems (2 errors, 0 warnings)


webpack compiled with 1 error

**解决办法:

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
//解决约束报错问题
  lintOnSave: false
})

6.路由跳转

 <router-link to="路径名称"></router-link>

gohome(){
this.$route.puth({
    path:"/home"
})
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3中,使用Vue Router进行路由拆分可以帮助你更好地组织和管理你的路由配置。以下是一些步骤来实现路由拆分: 1. 安装Vue Router:首先,确保你的项目中已经安装了Vue Router。可以通过以下命令进行安装: ``` npm install vue-router@next ``` 2. 创建路由模块:创建一个新的模块来定义和配置你的路由。可以创建一个名为`router.js`的文件,并在其中导入VueVue Router: ```javascript import { createRouter, createWebHistory } from 'vue-router'; const routes = [ // 定义你的路由配置 ]; const router = createRouter({ history: createWebHistory(), routes, }); export default router; ``` 3. 定义路由配置:在路由模块中,通过`routes`数组来定义你的路由配置。每个路由对象应该包含一个`path`属性和一个`component`属性,分别指定路由的路径和对应的组件。 ```javascript import Home from './components/Home.vue'; import About from './components/About.vue'; const routes = [ { path: '/', component: Home, }, { path: '/about', component: About, }, // 更多路由配置... ]; ``` 4. 在主应用程序中使用路由:在你的主应用程序中,导入并使用创建的路由实例。你可以在`main.js`文件中完成这个步骤: ```javascript import { createApp } from 'vue'; import App from './App.vue'; import router from './router'; const app = createApp(App); app.use(router); app.mount('#app'); ``` 现在,你已经成功拆分了你的路由配置。你可以在每个组件中使用`<router-link>`和`<router-view>`来进行导航和渲染路由组件。 注意:以上只是一个简单的示例,你可以根据你的项目需求进行更复杂的路由配置和组织方式。不过,以上步骤应该可以帮助你开始使用Vue Router进行路由拆分。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值