0
点赞
收藏
分享

微信扫一扫

vue路由传参的基础用法

嚯霍嚯 2022-02-07 阅读 56

vue路由的基础用法

1 .安装
 npm install vue-router --save
2 .main.js中
//Vue路由:引入
import VueRouter from 'vue-router'
Vue.use(VueRouter)

//Vue路由:引入并创建组件
import BYHome from './components/BYHome.vue'
import BYNews from './components/BYNews.vue'
import HelloWorld from './components/HelloWorld.vue'

//Vue路由:配置路由
const routes = [
  {path: '/home', component: BYHome},
  {path: '/news', component: BYNews},
  {path: '/helloworld', component: HelloWorld},
  {path: '*', redirect: '/home'} /*默认跳转路由 */
]

//Vue路由:实例化VueRouter 
const router = new VueRouter({
  routes //缩写,相当于 routes:routes
})

new Vue({
  router, //Vue路由:挂载路由
  render: h => h(App),
}).$mount('#app')

//Vue路由:根组件的模板里面放上下面这句话,需要在App.vue 中配置路由出口:路由匹配到的组件将渲染在根组件App.vue中
<router-view></router-view>

//路由跳转
<router-link to="/home">首页</router-link>  
<router-link to="/news">新闻</router-link>
<router-link to="/helloworld">helloWorld</router-link>

路由的跳转方式

第一种跳转方式:编程式导航

{path: '/news', component: BYNews},
this.$router.push({path:'news'});
// 带参:
{path: '/newDetail/:aid', component: BYNewDetail},
this.$router.push({path:'/newDetail/495'});

第二种跳转方式:命名路由

{path: '/news', component: BYNews,name:'news'},
this.$router.push({name:'news'});
// 带参:
this.$router.push({name:'news',params:{userId:123}});

路由模式

路由的hash模式以及history模式:

默认是hash模式,路由上方的路径是用#表示
http://localhost:8080/#/news

可以将hash模式改为history模式,路由上方的路径就没有了

路由的嵌套

//路由的配置
import BYUser from '../BYUser.vue'
import UserAdd from '../User/UserAdd.vue'
import UserList from '../User/UserList.vue'

{path: '/user' , component:BYUser,
    children:[
      {path: 'useradd',component:UserAdd},
      {path: 'userlist',component:UserList}
    ]
},
举报

相关推荐

0 条评论