路由(二)

一.路由跳转的replace方法

1.作用:控制路由跳转时操作浏览器历史记录的模式

浏览器的历史记录有两种写入方式:push和replace,其中push是追加历史记录,replace是替换当前记录。路由跳转时候默认为push方式


2.开启replace模式:

<router-link replace ...>News</router-link>

二.编程式路由导航

  1. 作用:不借助<router-link>实现路由跳转,让路由跳转更加灵活

  2. 具体编码

    this.$router.push({
    	name:'xiangqing',
        params:{
            id:xxx,
            title:xxx
        }
    })
    
    this.$router.replace({
    	name:'xiangqing',
        params:{
            id:xxx,
            title:xxx
        }
    })
    this.$router.forward() //前进
    this.$router.back() //后退
    this.$router.go() //可前进也可后退
    

三.缓存路由组件

  1. 作用:让不展示的路由组件保持挂载,不被销毁

//缓存一个路由组件
<keep-alive include="News"> //include中写想要缓存的组件名,不写表示全部缓存
    <router-view></router-view>
</keep-alive>

//缓存多个路由组件
<keep-alive :include="['News','Message']"> 
    <router-view></router-view>
</keep-alive>

四.两个生命周期钩子

  1. activateddeactivated是路由组件所独有的两个钩子,用于捕获路由组件的激活状态
  2. 具体使用:
    1. activated路由组件被激活时触发
    2. deactivated路由组件失活时触发
 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)
        }

五.路由守卫

5.1全局守卫

//全局前置路由守卫————初始化的时候、每次路由切换之前被调用
router.beforeEach((to,from,next) => {
    console.log('前置路由守卫',to,from)
    if(to.meta.isAuth){
        if(localStorage.getItem('school')==='atguigu'){
            next()
        }else{
            alert('学校名不对,无权限查看!')
        }
    }else{
        next()
    }
})

//全局后置路由守卫————初始化的时候被调用、每次路由切换之后被调用
router.afterEach((to,from)=>{
	console.log('后置路由守卫',to,from)
	document.title = to.meta.title || '硅谷系统'
})

5.2独享路由守卫

{
            name:'zhuye',
            path:'/home',
            component:Home,
            meta:{title:'主页'},
            children:[
                {
                    name:'xinwen',
                    path:'news',
                    component:News,
                    meta:{title:'新闻'},
                    //独享守卫,特定路由切换之后被调用
                    beforeEnter(to,from,next){
                        console.log('独享路由守卫',to,from)
                        if(localStorage.getItem('school') === 'atguigu'){
                            next()
                        }else{
                            alert('暂无权限查看')
                        }
                    }
                }

5.3组件内路由守卫

src/pages/About.vue:

<template>
    <h2>我是About组件的内容</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('学校名不对,无权限查看!')
            }
        },
        //通过路由规则,离开该组件时被调用
        beforeRouteLeave (to, from, next) {
            console.log('About--beforeRouteLeave',to,from)
            next()
        }
    }
</script>

六、路由器的两种工作模式

对于一个url来说,什么是hash值?—— #及其后面的内容就是hash值

hash值不会包含在 HTTP 请求中,即:hash值不会带给服务器

hash模式:

地址中永远带着#号,不美观
若以后将地址通过第三方手机app分享,若app校验严格,则地址会被标记为不合法
兼容性较好
history模式:

地址干净,美观
兼容性和hash模式相比略差
应用部署上线时需要后端人员支持,解决刷新页面服务端404的问题

connect-history-api-fallback

nginx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 React 中,使用 React Router 可以轻松地创建路由。下面是一个简单的示例来说明如何创建路由: 首先,确保你已经安装了 React Router 库。可以使用以下命令来安装: ``` npm install react-router-dom ``` 接下来,在你的应用程序中导入所需的模块: ```jsx import React from 'react'; import { BrowserRouter as Router, Route, Switch, Link } from 'react-router-dom'; ``` 然后,在你的组件中定义路由。下面是一个示例: ```jsx function App() { return ( <Router> <div> <nav> <ul> <li> <Link to="/">Home</Link> </li> <li> <Link to="/about">About</Link> </li> <li> <Link to="/dashboard">Dashboard</Link> </li> </ul> </nav> <Switch> <Route path="/" exact component={Home} /> <Route path="/about" component={About} /> <Route path="/dashboard" component={Dashboard} /> </Switch> </div> </Router> ); } function Home() { return <h2>Home</h2>; } function About() { return <h2>About</h2>; } function Dashboard() { return ( <div> <h2>Dashboard</h2> <Switch> <Route path="/dashboard/profile" component={Profile} /> <Route path="/dashboard/settings" component={Settings} /> </Switch> </div> ); } function Profile() { return <h3>Profile</h3>; } function Settings() { return <h3>Settings</h3>; } ``` 在上面的示例中,我们创建了一个 `Router` 组件,并在其中定义了导航菜单和路由规则。`Route` 组件用来定义路径和组件的映射关系。`Switch` 组件用来保证只有一个路由匹配成功。 在 `Dashboard` 组件中,我们创建了另一个嵌套的 `Switch` 组件,以支持路由。在这个嵌套的 `Switch` 中,我们定义了 `/dashboard/profile` 和 `/dashboard/settings` 这两个路径与对应的组件的映射关系。 最后,将 `App` 组件渲染到 DOM 中: ```jsx ReactDOM.render(<App />, document.getElementById('root')); ``` 通过以上代码,你就可以在你的应用程序中创建路由了。在导航菜单中点击链接将会渲染相应的组件。例如,点击 `/dashboard` 将会显示 `Dashboard` 组件,点击 `/dashboard/profile` 将会显示 `Profile` 组件。 希望这个示例能帮助你理解如何在 React 中创建路由。如有其他问题,请随时提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值