路由的基本使用
#和路径
模块化开发:
在router文件夹下
‘import { createRouter,createWebHashHistory } from 'vue-router ' import Film from '../views/film.vue' const routes=[ { path:"/films", component:Film } , path:"/center", component:Film } ] const router=createRouter({ history:createWebHashHistory(),//使用#路由 router, })
export defalut router
将来模块一进行调用之后,立即按照数组的配置,相当于将我们的路由对象创建完后,并且在main,js中进行注册使用,只要路径匹配到那个,就会进行那个组件的加载.
所以要在 1. main,js中进行router文件的导入
2.注册路由插件 app.use(router)
路由的用法
在app,vue文件下使用
在根组件中留有插槽,每当路由改变。就会加载相应的组件
相当于插槽slot
路由容器
不用引入和注册,已经被全局的引用和注册了
路由重定向和别名
重定向有两种写法:路径和别名
{
path:“/”,
redirect:“/films”
}
当路径是/时,直接重定向到/films下
命名路由
{
path:“/films”,
name:“myfilms”
component:Film
}
{
path:“/”,
redirect:{
name:“myfilms”
}
}
notfound,在router4下的其它路由匹配
{
path:“/:pathMatch(.)”,
component:notfound
}
别名
{
path:“/films”,
alias:“/wode”
component:Film
}