Vue----路由Vue router

根据官方文档说明安装:

npm:

npm install vue-router@4
​
cnpm install vue-router@4

​

yarn:

yarn add vue-router@4

我们可以从github上面下载写stars比较多的示例下来参考如何使用

挑选热度最高的,进去查看examples示例

将项目clone下来后打开,就可以参考里面vue-router的示例了。

查看示例demo:将demo使用到项目中

 项目中使用router:

        1.src中创建router目录,专用来存放路由相关信息;如果直接把路由数据都放在main.js文件中,main.js文件就变得非常臃肿。

        2.在router中创建index.js目录 。

        3.将demo中的示例copy到index.js中

// 0. 安装并导入vue-router
import { createWebHistory, createRouter } from "vue-router";
import home from "@/components/home.vue";
import about from "@/components/about.vue";
import Login from "@/components/login";

const routes = [
    {
        path: "/",
        name: "Home",
        component: home,
    },
    {
        path: "/about",
        name: "About",
        component: about,
    },
    {
        path: "/login",
        name: "Login",
        component: Login,
    },
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});
// 4. export关键字导出路由
export default router;

// 5. 在main.js入口文件中导入vue-router对象

// 6. 根组件App.vue中加载路由内容


        4. 在main.js入口文件中使用该router

import { createApp } from 'vue'
import App from './App.vue'
// 导入vue-router对象
import router from './router'
const app = createApp(App)
// 使用router
app.use(router)
app.mount('#app')

vue2和vue3中main.js入口文件的区别(左2,右3): 

        5. 在根组件App.vue中使用router-view导入路由内容

                <router-link to=" ">xxx</router-link>

6.浏览器中访问/路径,验证是否会访问到组件内容

7.记一次坑:

vue报错:Component name “xxx“ should always be multi-wordvue报错:Component name “xxx“ should always be multi-word        

原因:语法提示的锅

解决:
在 vue.config.js 中关闭语法提示:

module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave: false, //关闭eslint检查
})

8.组件中获取前端传递过来的字符串参数/路径参数:

        this.$route.query 获取字符串参数

        this.$route.params 获取路径参数

示例:

login组件:

<template>

    <p>model中的用户名为:{{ruleForm.username}}</p>
    <p>model中的密码为:{{ruleForm.passwd}}</p>


</template>

<script>
export default {
  name: "login",
  data(){
    return {
      labelPosition:"left",
      ruleForm:{
        username:"chuntian",
        passwd:123456
      }
    }
  },
  created() {
    console.log("实例创建时,会触发created钩子函数中的内容")
    this.ruleForm.username = this.$route.query.name
    this.ruleForm.passwd = this.$route.query.pwd
  },
  mounted() {
    console.log("挂载后,会触发mounted钩子函数中的内容")
  }

}
</script>

<style scoped>
.demo-ruleForm{
  width:350px;
  margin:50px auto;
}


</style>

效果:

嵌套路由(子路由):

        在父路由下再添加子路由

        父路由中通过children:[{},{}]来绑定子路由

import { createWebHistory, createRouter } from "vue-router";
import home from "@/components/home.vue";
import about from "@/components/about.vue";
import Login from "@/components/login";

const routes = [
    {
        path: "/login",
        name: "Login",
        component: Login,
        children:[
            {path:'',name:'About',component:about},
            {path:'/home',name:'Home',component:home},
        ]
    },
];

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

export default router;

路由是从上到下匹配

在子组件上使用<router-view></router-view>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chuntian_tester

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值