router的使用
路由和線路
路由router
表示當前項目全局的路由實例對象
跳轉方法:push,replace,go,back
線路route
表示當前路由頁面的信息對象
獲取動態路由的參數:params
router跳轉的兩種方式
js跳轉叫[編程式跳轉]
<button @click="fn_target"></button>
標簽跳轉叫【聲明式】
<router-link to="/name"></router-link>
router-link
特性 默認是會被渲染成a標簽 可以用tag屬性修改
屬性值
- to屬性跳轉到哪個頁面和path對應 (相當于執行一次this.$router.push(’/name’))
- replace屬性,不需要寫值,讓頁面不可回退,默認是push屬性
- active-class用于修改單個class屬性
- tag渲染成什么元素
router-view
router-view決定渲染組件位置
routes簡單語法
【注意】:不要忘記引入組件
const routes = [{path: '',// redirect重定向 ,相當于直接默認了home頁面redirect: '/home'},{path: ' * ',// 當訪問組件不存在時默認返回home組件redirect: '/home'},{//路由嵌套(也需要一個視口) 地址顯示為 /home/name//【注意】子路由路徑中不加 / 程序自動拼接path: '/home',component: Home,children: [//重定向{ path: '', redirect: 'new' },{path: 'new',component: New,},{path: 'product',component: Product,}]},{//動態路由path: '/User/:userId',//渲染時:this.$router.push(`/info/value`)component: User},// 路由懶加載(不用引入組件) 分割js文件(一個懶加載對應一個js文件),避免用戶加載頁面會出現短暫空白//方式一:結合Vue異步組件和webpack的代碼分析(知道即可,老項目有可能會出現)//const Home = resolve => { require.ensure(['../components/Home.vue],()=>{//resolve(require('../components/Home.vue')) })}//方式二:AMD寫法//const About = resolve =>require(['../components/About.vue'],resolve)//方式三:在ES5中,我們可以有更加簡單的寫法來組織Vue異步組件和Webpack的代碼分割{ path: '/home',component: ()=>import('../components/Home')} ]router簡單使用
const router = new VueRouter({routes,// 默認hash值,現在改成history模式mode: 'history',// 修改全局的classlinkActiveClass: 'active' })路由傳參的兩種方式
params
- 隱式(不推薦) 靜態路由使用params傳參,地址欄不會帶參數,所以刷新頁面數據會丟,一般不使用
- 顯示-動態路由傳參。且必須使用路由命名的name屬性,不可以使用path。使用params對象傳遞參數
query
使用query傳參,地址欄以get請求參數的形式表現。他沒有動態路由優雅
// routes配置中要有相應的path this.$router.push({path:'/home',query:{id:1}})總結
- 上一篇: 权威预测:未来一年,企业云服务将会如何发
- 下一篇: 电脑备份无法启动不了怎么办 电脑备份故障