首页 > 其他分享 >vue-router

vue-router

时间:2022-12-03 13:00:12浏览次数:39  
标签:vue name home top import router

1.安装

npm install --legacy-peer-deps [email protected]

要指定版本号 否则会报错

2. 使用

2.1 在src下创建router目录,在其中新建index.js文件

import VueRouter from "vue-router";
//引入一个路由组件
import Home from '../pages/Home'
 
//创建一个路由并暴露出去 注意component没有引号
export default new VueRouter({
    routes:[
        {
            name:'home',
            path:'/home',
            component:Home
        }]
})

2.2 main.js

// 引入路由
import VueRouter from 'vue-router'
import router from './router'
​
Vue.use(VueRouter)
​
new Vue({
  render: h => h(App),
  router
}).$mount('#app')

2.3 App.vue

<template>
  <div id="app">
	<router-link to="/home">home</router-link>
	<router-view></router-view>
  </div>
</template>

2.4

  • router-link 来创建链接
  • router-view 将显示与 url 对应的组件,放在需要的地方

3.嵌套路由

3.1 index.js

import left from '../pages/panel/Left'
import top from '../pages/panel/top'

routes:[{
	name:'home',
        path:'/home',
        component:home,
	children:[
	{
	  name:'left',
	  path:'/left/:id',
	  component:left,
	  props:true
	},{
	  name:'top',
	  path:'/top',
	  component:top,
	}]

3.2 left和top内容页

<template>
  <div>
    内容2
  </div>
</template>

<script>
  export default{
    name:"top"
  }
</script>
<template>
  <div>
    内容1
  </div>
</template>

<script>
  export default{
    name:"left",
  }
</script>

3.3 home.vue

<template>
  <router-link to="/left">选项1</router-link>
  <router-link to="/top">选项2</router-link>
  <!-- 嵌套路由,点击不同连接 产生的内容页不同 -->
  <el-main>
      <router-view></router-view>
  </el-main>
</template>

3.4 效果

点击不同路由连接,产生的内容页不同

4.参数传递

标签:vue,name,home,top,import,router
From: https://www.cnblogs.com/lwx11111/p/16947358.html

相关文章

  • uni 结合vuex 编写动态全局配置变量 this.baseurl
    在日常开发过程,相信大家有遇到过各种需求,而我,在这段事件便遇到了一个,需要通过用户界面配置动态接口,同时,因为是app小程序开发,所以接口中涉及到了http以及websocket两个类型......
  • vue 添加多条数据 添加日期
    效果图添加多条数据,日期是具体到天。   后端数据格式time:[{s_time:'',e_time:''}]<pv-for="(item,index)inform.third_extend":key="index"style=......
  • vue scss样式预处理在 vscode 中起作用,但是报错
      2.在最外层级,输入代码  "files.associations":{"*.vue":"vue"} ......
  • vue实现按钮多选
    需求是这样: 首先考虑使用elementui中的组建实现,但是有时候会忽略组建。实现方式两种:1.直接使用element实现letweekTimeData:['星期一','星期二','星......
  • Vue的指令与过滤器
    1.内容渲染指令v-text覆盖标签原有内容{{}}插值表达式v-html2.属性绑定指令v-bind为属性动态绑定值简写':'3.事件绑定指令v-on简写'@'vue提供了内置......
  • vue 项目开发记录1
    ---------------------------jbs开发记录---------------------------1,新建vue-cli项目2,win7不能使用node14以上的版本 1,使用save会在package.json中自动添加。npmi......
  • vue run build打包之后服务器端访问图片404
    记录:assetsRoot:path.resolve(__dirname,BUILD_RESOURCES_DIST),//打包后文件存放的路径assetsSubDirectory:"",//除了index.html之外的静态资源要存放的路径assets......
  • vue3 + vite 监听路由
    1.watch监听import{watch}from'vue'import{useRouter}from'vue-router'letrouter=useRouter()watch(()=>router.currentRoute._value,(m,n)=>{......
  • Vue.js_Vue Router 4.x 动态路由解决刷新空白
    问题描述:基于对VueRouter3.x没有改变前,我们常规的实现一定,在store中根据获取的用户权限,对路由进行过滤并返回,然后到路由守卫的地方,使用addRoutes动态添加路由。但......
  • vue文件分片上传,断点续传
    ​ 1 背景用户本地有一份txt或者csv文件,无论是从业务数据库导出、还是其他途径获取,当需要使用蚂蚁的大数据分析工具进行数据加工、挖掘和共创应用的时候,首先要将本地文......