国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Vue-Router相關(guān)理解4

這篇具有很好參考價值的文章主要介紹了Vue-Router相關(guān)理解4。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

兩個新的生命周期鉤子

activateddeactivated是路由組件所獨有的兩個鉤子,用于捕獲路由組件的激活狀態(tài)具體使用

activated路由組件被激活時觸發(fā)

deactivated路由組件失活時觸發(fā)

Vue-Router相關(guān)理解4,vue,vue.js,前端,javascript?

src/pages/News.vue

<template>
  <ul>
    <li :style="{opacity}">歡迎學(xué)習(xí)Vue</li>
    <li>news001 <input type="text"></li>
    <li>news002 <input type="text"></li>
    <li>news003 <input type="text"></li>
  </ul>
</template>

<script>
export default {
  name: 'News',
  data() {
    return {
      opacity: 1,
    }
  },
  activated() {
    console.log('News組件激活了');
    this.timer = setInterval(() => {
      this.opacity -= 0.01
      if (this.opacity <= 0) this.opacity = 1
    }, 16)
  },
  deactivated() {
    console.log('News組件失活了');
    clearInterval(this.timer)
  }
}
</script>

路由守衛(wèi)

對路由權(quán)限進行控制

meta元數(shù)據(jù)對象

全局路由守衛(wèi)

src/router/index.js

import VueRouter from 'vue-router'
// 引入組件
import About from '../pages/About'
import Home from '../pages/Home'
import News from '../pages/News'
import Message from '../pages/Message'
import Detail from '../pages/Detail'

//創(chuàng)建并暴露一個路由器
const router= new VueRouter({
    routes: [
        {
            name:'guanyu',
            path: '/about',
            component: About,
            meta:{title:'關(guān)于'}
        },
        {
            name:'zhuye',
            path: '/home',
            component: Home,
            meta:{title:'主頁'},
            children: [
                {
                    name:'xinwen',
                    path: 'news',
                    component: News,
                    meta:{isAuth:true,title:'新聞'}
                },
                {
                    name:'xiaoxi',
                    path: 'message',
                    component: Message,
                    meta:{isAuth:true,title:'消息'},
                    children: [
                        {
                            name:'xiangqing',
                            // path: 'detail/:id/:title',  //使用占位符聲明接收params參數(shù)
                            path:'detail',  //使用query參數(shù)
                            component: Detail,
                            meta:{isAuth:true,title:'詳情'},
                            //第一種寫法:props值為對象,該對象中所有的key-value的組合都會通過props傳給Detail組件
                            // props:{a:'100',b:'1'},
                            //第二種寫法:props值為true,則把路由收到的所有params參數(shù)通過props傳給Detail組件
                            // props:true,
                            //第三種寫法:props值為函數(shù),該函數(shù)返回的對象中每一組key-value都會通過props傳給Detail組件
                            props($route){
                                return{
                                    id:$route.query.id,
                                    title:$route.query.title
                                }
                            }
                        }
                    ]
                }
            ]
        }
    ]
})

// 全局前置路由守衛(wèi)--初始化的時候、每次路由切換之前被調(diào)用
router.beforeEach((to,from,next)=>{
    console.log('beforeEach',to,from)
    if(to.meta.isAuth){ //判斷是否需要鑒權(quán)
        if(localStorage.getItem('school')==='atguigu'){
            next()
        }else{
            alert('學(xué)校名不對,無權(quán)限查看!')
        }
    }else{
        next()
    }
})

// 全局路由后置守衛(wèi)--初始化的時候、每次路由切換之后被調(diào)用
router.afterEach((to,from)=>{
    console.log('afterEach',to,from)
    document.title=to.meta.title||'硅谷系統(tǒng)'
})

export default router

獨享路由守衛(wèi)beforeEnter

src/router/index.js?

import VueRouter from 'vue-router'
// 引入組件
import About from '../pages/About'
import Home from '../pages/Home'
import News from '../pages/News'
import Message from '../pages/Message'
import Detail from '../pages/Detail'

//創(chuàng)建并暴露一個路由器
const router = new VueRouter({
    routes: [
        {
            name: 'guanyu',
            path: '/about',
            component: About,
            meta: { title: '關(guān)于' }
        },
        {
            name: 'zhuye',
            path: '/home',
            component: Home,
            meta: { title: '主頁' },
            children: [
                {
                    name: 'xinwen',
                    path: 'news',
                    component: News,
                    meta: { isAuth: true, title: '新聞' },
                    // 獨享前置路由守衛(wèi)
                    beforeEnter: (to, from, next) => {
                        console.log('beforeEach', to, from)
                        if (to.meta.isAuth) { //判斷是否需要鑒權(quán)
                            if (localStorage.getItem('school') === 'atguigu') {
                                next()
                            } else {
                                alert('學(xué)校名不對,無權(quán)限查看!')
                            }
                        } else {
                            next()
                        }
                    }
                },
                {
                    name: 'xiaoxi',
                    path: 'message',
                    component: Message,
                    meta: { isAuth: true, title: '消息' },
                    children: [
                        {
                            name: 'xiangqing',
                            // path: 'detail/:id/:title',  //使用占位符聲明接收params參數(shù)
                            path: 'detail',  //使用query參數(shù)
                            component: Detail,
                            meta: { isAuth: true, title: '詳情' },
                            //第一種寫法:props值為對象,該對象中所有的key-value的組合都會通過props傳給Detail組件
                            // props:{a:'100',b:'1'},
                            //第二種寫法:props值為true,則把路由收到的所有params參數(shù)通過props傳給Detail組件
                            // props:true,
                            //第三種寫法:props值為函數(shù),該函數(shù)返回的對象中每一組key-value都會通過props傳給Detail組件
                            props($route) {
                                return {
                                    id: $route.query.id,
                                    title: $route.query.title
                                }
                            }
                        }
                    ]
                }
            ]
        }
    ]
})

// 全局路由后置守衛(wèi)--初始化的時候、每次路由切換之后被調(diào)用
router.afterEach((to, from) => {
    console.log('afterEach', to, from)
    document.title = to.meta.title || '硅谷系統(tǒng)'
})

export default router

Vue-Router相關(guān)理解4,vue,vue.js,前端,javascript?

組件內(nèi)路由守衛(wèi)

從/home跳轉(zhuǎn)到/about前觸發(fā)beforeRouteEnter,從/about跳轉(zhuǎn)到/test前觸發(fā)beforeRouteLeave

Vue-Router相關(guān)理解4,vue,vue.js,前端,javascript

src/pages/About.vue

<template>
  <h2>我是About的內(nèi)容</h2>
</template>

<script>
export default {
  name: 'About',
  beforeRouteEnter (to, from, next) {
    console.log('About--beforeRouteEnter',to,from);
    if(localStorage.getItem('school')==='atguigu'){
      next()
    }else{
      alert('學(xué)生名不對,無權(quán)查看!')
    }
  },
  beforeRouteLeave(to, from, next){
    console.log('About--beforeRouteLeave',to,from);
    next()
  }
}
</script>

路由器的兩種工作模式

Vue-Router相關(guān)理解4,vue,vue.js,前端,javascript?

const router=new VueRouter({
    mode:'history',
    routes:[...]
})

export default router

項目打包npm run build

demo目錄下

第一步:npm init出現(xiàn)package name:atguigu_test_server

第二步:npm i express

第三步:打開npmjs.com網(wǎng)站搜索connect-history-api-fallback

npm i connect-history-api-fallback

Vue-Router相關(guān)理解4,vue,vue.js,前端,javascript?

?文章來源地址http://www.zghlxwxcb.cn/news/detail-594665.html

?

到了這里,關(guān)于Vue-Router相關(guān)理解4的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • vue-router.esm.js:2248 Error: Cannot find module ‘@/views/dylife/ 報錯解決
  • vue-router.esm.js?a12b:2046 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation

    vue-router.esm.js?a12b:2046 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation

    vue-router.esm.js?a12b:2046 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: \\\"/home\\\". ? 報錯原因:重復(fù)點擊路由導(dǎo)致,因為vue-router引入了promise,當(dāng)我們使用this.$router.push時候需要多添加成功或失敗的回調(diào),否則就會報出以上的錯誤。 原代碼 第一種 ? ? ? ? 在進

    2023年04月08日
    瀏覽(186)
  • vue-router3.x和vue-router4.x相互影響的問題記錄

    vue-router3.x和vue-router4.x相互影響的問題記錄

    項目中有一個系統(tǒng)使用的微前端,主站使用是 vue2 實現(xiàn)的,使用的是 vue-router3.x 。子應(yīng)用有使用 vue3 實現(xiàn)的,使用的為 vue-router4.x 。 該子應(yīng)用中的頁面A有通過操作按鈕觸發(fā)跳轉(zhuǎn)到其他子應(yīng)用頁面B的需求,此時使用的是 vue-router4.x 的編程式導(dǎo)航API。 當(dāng)通過點擊主站的Tab切換回

    2023年04月26日
    瀏覽(18)
  • 路由,vue-router的基本用法,vue-router的常見用法$route.params、$router.push、$router.replace、$router.go

    路由,vue-router的基本用法,vue-router的常見用法$route.params、$router.push、$router.replace、$router.go

    路由(英文: router)就是 對應(yīng)關(guān)系 。 SPA指的是一個web網(wǎng)站只有唯一的一個HTML頁面, 所有組件的展示與切換 都在這唯一的一個頁面內(nèi)完成。此時, 不同組件之間的切換 需要通過 前端路由 來實現(xiàn)。 *結(jié)論:*在SPA項目中, 不同功能之間的切換 ,要 依賴于前端路由 來完成! 通俗

    2024年01月16日
    瀏覽(27)
  • 路由vue-router

    路由vue-router

    路由(英文:router)就是 對應(yīng)關(guān)系 SPA 指的是一個 web 網(wǎng)站只有 唯一的一個 HTML 頁面 ,所有組件的展示與切換都在這唯一的一個頁面內(nèi)完成。 此時,不同組件之間的切換需要通過前端路由來實現(xiàn)。 結(jié)論: 在 SPA 項目中,不同功能之間的切換 ,要依賴于 前端路由 來完成!

    2024年02月07日
    瀏覽(24)
  • vue-router筆記

    目的:為了實現(xiàn)SPA(單頁面應(yīng)用) vue-router是一個插件庫 安裝: 路由的配置路徑:/src/router/index.js 路由組件的目錄:/src/pages/ 一般組件的目錄:/src/components/ 使用: main.js: App.vue: About.vue: 不可見的路由組件在哪?? ? ? ? 被銷毀了 嵌套路由的案例: 路由傳參: query: 傳參

    2024年02月12日
    瀏覽(29)
  • 【Vue-Router】別名

    【Vue-Router】別名

    后臺返回來的路徑名不合理,但多個項目在使用中了,不方便改時可以使用別名??梢杂卸鄠€或一個。 First.vue Second.vue , Third.vue 代碼同理 UserSettings.vue index.ts App.vue

    2024年02月12日
    瀏覽(20)
  • Uncaught SyntaxError: The requested module ‘/node_modules/.vite/deps/vue-router.js?v=9eef87ba‘

    錯誤代碼 錯誤提示 瀏覽器中路由無法顯示,提示錯誤 問題描述 解決方法

    2024年02月05日
    瀏覽(107)
  • Vue3配置路由(vue-router)

    Vue3配置路由(vue-router)

    緊接上篇文章,vue3的配置與vue2是有所差別的,本文就講述了如何配置,如果本文對你有所幫助請三連支持博主。 下面案例可供參考 使用npm命令進行安裝 : npm install vue-router@4 完成后我們打開項目根目錄下的 package.json 文件: 如下即為成功 這里創(chuàng)建 view目錄,然后在view目錄

    2023年04月12日
    瀏覽(27)
  • VUE-CLI/VUE-ROUTER

    VUE-CLI/VUE-ROUTER

    個人簡介 ??個人主頁:是Lay的主頁 ??學(xué)習(xí)方向:JAVA后端開發(fā)? ??種一棵樹最好的時間是十年前,其次是現(xiàn)在! ?往期文章:【Java基礎(chǔ)】面向?qū)ο筮M階(二) ??喜歡的話麻煩點點關(guān)注喔,你們的支持是我的最大動力。 目錄 ? 前言: 1. 安裝 1.1 npm安裝 1.2 vue-cli安裝 2. 初始化項

    2024年01月16日
    瀏覽(72)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包