亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue-router編程式導(dǎo)航的兩種實(shí)現(xiàn)代碼

 更新時(shí)間:2021年03月04日 14:38:18   作者:愛發(fā)呆的程序員  
這篇文章主要介紹了Vue-router編程式導(dǎo)航的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

頁(yè)面導(dǎo)航的兩種方式

聲明式導(dǎo)航:通過點(diǎn)擊鏈接實(shí)現(xiàn)導(dǎo)航的方式,叫做聲明式導(dǎo)航
例如:普通網(wǎng)頁(yè)中的 <a></a> 鏈接 或 vue 中的 <router-link></router-link>
編程式導(dǎo)航:通過調(diào)用JavaScript形式的API實(shí)現(xiàn)導(dǎo)航的方式,叫做編程式導(dǎo)航
例如:普通網(wǎng)頁(yè)中的 location.href

編程式導(dǎo)航基本用法

常用的編程式導(dǎo)航 API 如下:

this.$router.push(‘hash地址')

this.$router.go(n)

 const User = {  
 		template: '<div><button @click="goRegister">跳轉(zhuǎn)到注冊(cè)頁(yè)面</button></div>',  
  	methods: { 
  	 goRegister: function(){   
    // 用編程的方式控制路由跳轉(zhuǎn)   
    	this.$router.push('/register'); 
  } 
  } 
 } 

具體嗎實(shí)現(xiàn):

<!DOCTYPE html>
<html lang="en">
 <head>
 <meta charset="UTF-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <meta http-equiv="X-UA-Compatible" content="ie=edge" />
 <title>Document</title>
 <!-- 導(dǎo)入 vue 文件 -->
 <!-- <script src="./lib/vue_2.5.22.js"></script> -->
 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
 <!-- <script src="./lib/vue-router_3.0.2.js"></script> -->
 <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
 </head>
 <body>
 <!-- 被 vm 實(shí)例所控制的區(qū)域 -->
 <div id="app">
  <router-link to="/user/1">User1</router-link>
  <router-link to="/user/2">User2</router-link>
  <router-link :to="{ name: 'user', params: {id: 3} }">User3</router-link>
  <router-link to="/register">Register</router-link>

  <!-- 路由占位符 -->
  <router-view></router-view>
 </div>

 <script>
  const User = {
  props: ['id', 'uname', 'age'],
  template: `<div>
   <h1>User 組件 -- 用戶id為: {{id}} -- 姓名為:{{uname}} -- 年齡為:{{age}}</h1>
   <button @click="goRegister">跳轉(zhuǎn)到注冊(cè)頁(yè)面</button>
  </div>`,
  methods: {
   goRegister() {
   this.$router.push('/register')//編程式導(dǎo)航
   }
  },
  }

  const Register = {
  template: `<div>
   <h1>Register 組件</h1>
   <button @click="goBack">后退</button>
  </div>`,
  methods: {
   goBack() {
   this.$router.go(-1)
   }
  }
  }

  // 創(chuàng)建路由實(shí)例對(duì)象
  const router = new VueRouter({
  // 所有的路由規(guī)則
  routes: [
   { path: '/', redirect: '/user' },
   {
   // 命名路由
   name: 'user',
   path: '/user/:id',
   component: User,
   props: route => ({ uname: 'zs', age: 20, id: route.params.id })
   },
   { path: '/register', component: Register }
  ]
  })

  // 創(chuàng)建 vm 實(shí)例對(duì)象
  const vm = new Vue({
  // 指定控制的區(qū)域
  el: '#app',
  data: {},
  // 掛載路由實(shí)例對(duì)象
  // router: router
  router
  })
 </script>
 </body>
</html>

router.push() 方法的參數(shù)規(guī)則

 // 字符串(路徑名稱) 
router.push('/home') 
// 對(duì)象 
router.push({ path: '/home' }) 
// 命名的路由(傳遞參數(shù)) 
router.push({ name: '/user', params: { userId: 123 }}) 
// 帶查詢參數(shù),變成 /register?uname=lisi 
router.push({ path: '/register', query: { uname: 'lisi' }}) 

到此這篇關(guān)于Vue-router編程式導(dǎo)航的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)Vue router編程式導(dǎo)航內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論