当前位置: 首页 > news >正文

珠海企业建站广东短视频seo搜索哪家好

珠海企业建站,广东短视频seo搜索哪家好,游戏app软件开发公司,北京通州马桥网站建设,前端路由的核心是什么呢?改变URL,但是页面不进行整体的刷新。 vue-router是基于路由和组件的  路由用于设定访问路径, 将路径和组件映射起来;  在vue-router的单页面应用中, 页面的路径的改变就是组件的切换; 使用router需要…

,前端路由的核心是什么呢?改变URL,但是页面不进行整体的刷新。

vue-router是基于路由和组件的
 路由用于设定访问路径, 将路径和组件映射起来;
 在vue-router的单页面应用中, 页面的路径的改变就是组件的切换;

使用router需要

1.在router文件夹内书写整个index.js文件

2.在View文件夹内书写具体的router文件内容

3.在App.vue文件夹想要替换内容的地方,写上路由占位符

4.main.js文件内import router from "./router",并且在mount app前app.use(router)

路由的使用步骤

◼ 使用vue-router的步骤:
 第一步:创建路由需要映射的组件(打算显示的页面);
 第二步:通过createRouter创建路由对象,并且传入routes和history模式;
✓ 配置路由映射: 组件和路径映射关系的routes数组;
✓ 创建基于hash或者history的模式;
 第三步:使用app注册路由对象(use方法);
 第四步:路由使用: 通过<router-link>和<router-view>;

 0.报错:无法正常渲染router

报错1:

必须把use router写在mount前 

报错2:

 要么是router-link没写to的路径,要么是  history:createWebHistory,这里没写()

1.简单使用

router文件夹index.js

import { createRouter,createWebHashHistory } from "vue-router";import Home from "../Views/Home.vue"
import About from "../Views/About.vue"const router = createRouter({//指定采用的模式:hashhistory:createWebHashHistory(),//映射关系routes:[{path:"/home",component:Home},{path:"/about",component:About},]
})export default router

Views文件夹下About与Home.vue文件

<template><h2>about</h2>
</template>
<template><h2>home</h2>
</template>

App.vue

<template>
<div class="app"><router-view></router-view><h2>App content</h2>
</div>
</template><script setup></script><style lang="less" scoped></style>

main.js

import { createApp } from 'vue'
// import "./assets/reset.css"
import router from "./router"import App from './App.vue'const app = createApp(App)// 全局注册app.use(router)app.mount('#app')

2.如果希望点击某个区域进行跳转:router-link

<template>
<div class="app"><h2>App content</h2><div class="nav"><router-link to="/home">首页</router-link><router-link to="/about">关于</router-link></div><router-view></router-view>
</div>
</template><script setup></script><style lang="less" scoped></style>

 3.路由的默认路径

 默认情况下, 进入网站的首页, 我们希望<router-view>渲染首页的内容;
 但是我们的实现中, 默认没有显示首页组件, 必须让用户点击才可以;
◼ 如何可以让路径默认跳到到首页, 并且<router-view>渲染首页组件呢?
◼ 我们在routes中又配置了一个映射:
 path配置的是根路径: /
 redirect是重定向, 也就是我们将根路径重定向到/home的路径下, 这样就可以得到我们想要的结果了.

const router = createRouter({//指定采用的模式:hashhistory:createWebHashHistory(),//映射关系routes:[{path:"/",redirect:"/home"},{path:"/home",component:Home},{path:"/about",component:About},]
})

4.history模式

使用WebHashHistory哈希模式,域名跳转时会有#,如果切成history则是日常的域名模式

import { createRouter,createWebHashHistory,createWebHistory } from "vue-router";import Home from "../Views/Home.vue"
import About from "../Views/About.vue"const router = createRouter({//指定采用的模式:hash// history:createWebHashHistory(),history:createWebHistory(),//映射关系routes:[{path:"/",redirect:"/home"},{path:"/home",component:Home},{path:"/about",component:About},]
})export default router

5.router-link的属性:to/replace/active/name

◼ router-link事实上有很多属性可以配置:
◼ to属性:
 是一个字符串,或者是一个对象

对象写法,记得:to,几乎不在to内使用对象写法

			<router-link :to="{path:'/about'}">关于</router-link>

◼ replace属性:
 设置 replace 属性的话,当点击时,会调用 router.replace(),而不是 router.push();

	<div class="nav"><router-link to="/home" replace>首页</router-link><router-link to="/about" replace>关于</router-link></div>

如果使用了replace属性,比如从百度进入8080页面,默认进入首页,此时点击进入关于界面,再点击返回,此时返回的不再是首页,而是百度页面

因为相当于百度→首页,关于页面替换了首页,此时百度→关于,所以点击返回按钮,返回百度界面

◼ active-class属性:
 设置激活a元素后自动应用的class,默认是router-link-active


◼ exact-active-class属性:
 链接精准激活时,应用于渲染的 <a> 的 class,默认是router-link-exact-active;

 通过自动绑定的active类,可以对其进行样式操作

.router-link-active 
{color: red;
}

◼ name属性:路由记录独一无二的名称;
◼ meta属性:自定义的数据 

6.路由懒加载

◼ 当打包构建应用时,JavaScript 包会变得非常大,影响页面加载:
 如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就会更加高效;
 也可以提高首屏的渲染效率;


◼ 其实这里还是我们前面讲到过的webpack的分包知识,而Vue Router默认就支持动态来导入组件:
 这是因为component可以传入一个组件,也可以接收一个函数,该函数 需要放回一个Promise;
 而import函数就是返回一个Promise;

默认 npm run build不分包(此写法几乎被摒弃

import Home from "../Views/Home.vue"
import About from "../Views/About.vue"

 如果想要分包:

const Home=()=>import ("../Views/About.vue")
const About=()=>import ("../Views/Home.vue")

如果希望知道到底每个包对应的内容 

使用webpack的魔法注释,webpack从3.x开始支持对分包进行命名(chunk name)

const Home=()=>import (/* webpackChunkName: 'home' */"../Views/About.vue")
const About=()=>import (/* webpackChunkName: 'about' */"../Views/Home.vue")

 但是现在最广泛的写法是:

import { createRouter,createWebHashHistory,createWebHistory } from "vue-router";// import Home from "../Views/Home.vue"
// import About from "../Views/About.vue"
// const Home=()=>import (/* webpackChunkName: 'home' */"../Views/About.vue")
// const About=()=>import (/* webpackChunkName: 'about' */"../Views/Home.vue")const router = createRouter({//指定采用的模式:hash// history:createWebHashHistory(),history:createWebHistory(),//映射关系routes:[{path:"/",redirect:"/home"},{path:"/home",component:()=>import ("../Views/Home.vue")},{path:"/about",component:()=>import ("../Views/About.vue")},]
})export default router

7.动态路由基本匹配


◼ 很多时候我们需要将给定匹配模式的路由映射到同一个组件:
 例如,我们可能有一个 User 组件,它应该对所有用户进行渲染,但是用户的ID是不同的;
 在Vue Router中,我们可以在路径中使用一个动态字段来实现,我们称之为 路径参数;

如果进入user页面,一般user界面会跟着id,但是我们默认的时user,此时无法匹配,无法进入user页面

    {path:"/user/:id", 

const router = createRouter({history:createWebHistory(),//映射关系routes:[{path:"/",redirect:"/home"},{path:"/home",component:()=>import ("../Views/Home.vue")},{path:"/about",component:()=>import ("../Views/About.vue")},{path:"/user/:id",component:()=>import ("../Views/User.vue")},]
})export default router

<template>
<div class="app"><h2>App content</h2><div class="nav"><router-link to="/home">首页</router-link><router-link to="/about">关于</router-link><router-link to="/user">用户</router-link><router-link to="/user/321">用户321</router-link><router-link to="/user/123">用户123</router-link></div><router-view></router-view>
</div>
</template>

 8.获取动态路由的值

◼ 那么在User中如何获取到对应的值呢?
 在template中,直接通过 $route.params获取值;
✓ 在created中,通过 this.$route.params获取值;
✓ 在setup中,我们要使用 vue-router库给我们提供的一个hook useRoute;
➢ 该Hook会返回一个Route对象,对象中保存着当前路由相关的值;

法一:在template中,直接通过 $route.params获取值;

<template><h2 class="user">User:{{$route.params}}</h2>
</template>
	{path:"/user/:id",component:()=>import ("../Views/User.vue")},

<template><h2 class="user">User:{{$route.params.id}}</h2>
</template>

法二:在setup中,我们要使用 vue-router库给我们提供的一个hook useRoute;

<template><h2 class="user">User:{{$route.params.id}}</h2>
</template><script setup>import {useRoute} from 'vue-router'const route =useRoute()console.log(route.params.id)
</script>

 但是这种写法在

			<router-link to="/user/321">用户321</router-link><router-link to="/user/123">用户123</router-link>

切换时不会控制台输出

如果想要改变:(很少用到)

<template><h2 class="user">User:{{$route.params.id}}</h2>
</template><script setup>import { useRoute, onBeforeRouteUpdate } from 'vue-router'const route = useRoute()console.log(route.params.id)// 获取route跳转idonBeforeRouteUpdate((to, from) => {console.log("from:", from.params.id)console.log("to:", to.params.id)})</script>

 法三:


 9.notfound

对于哪些没有匹配到的路由,我们通常会匹配到固定的某个页面
 比如NotFound的错误页面中,这个时候我们可编写一个动态路由用于匹配所有的页面;

 这里path:"/:patMatch(.*)"意味着匹配到所有路径

但是home/user/about这种还是会正常跳转的

	{path:"/:patMatch(.*)",component:()=>import ("../Views/NotFound.vue")}
<template><div class="not-found"><h2>NotFound:您当前的路径不正确</h2></div>
</template><script setup></script><style scoped>
</style>

获得具体不正确的路径 

<template><div class="not-found"><h2>NotFound:您当前的路径不正确{{$route.params.patMatch}}</h2></div>
</template>

10.编程式路由跳转 

使用费router-link跳转需要用到编程式

使用push的特点是压入一个新的页面,那么在用户点击返回时,上一个页面还可以回退,但是如果我们希望当前页面是一个替换操作,那么可以使用replace: 

<template>
<div class="app"><h2>App content</h2><div class="nav"><span @click="homeSpanClick">首页</span><button @click="aboutBtnClick">关于</button></div><router-view></router-view>
</div>
</template><script setup>import {useRouter} from 'vue-router'const router=useRouter()function homeSpanClick(){router.push("/home")}function aboutBtnClick(){router.push("/about")}
</script>
		import {useRouter} from 'vue-router'const router=useRouter()function homeSpanClick(){router.push("/home")}

获取当前正在使用的router 

		function homeSpanClick(){router.push({path:"/home"})}

这样写可以写更多参数

 11.页面的前进后退

<template><h2 class="about">about</h2><button @click="backBtnClick">返回</button>
</template><script setup>import {useRouter} from 'vue-router'const router=useRouter()function backBtnClick(){router.back()}
</script>

12.动态添加路由

◼ 某些情况下我们可能需要动态的来添加路由:
 比如根据用户不同的权限,注册不同的路由;
 这个时候我们可以使用一个方法 addRoute;

let isAdmin=true
if(isAdmin)
{router.addRoute(	{path:"/admin",component:()=>import ("../Views/Admin.vue")},)
}export default router

//动态管理路由
let isAdmin=true
if(isAdmin)
{router.addRoute(	{path:"/admin",component:()=>import ("../Views/Admin.vue")},)//添加vip页面router.addRoute({path: "/home/vip",component: () => import("../Views/HomeVip.vue")});
}

动态添加二级路由 

 13.删除路由

name必须是唯一的

 14.路由导航守卫

比如在首页点击order按钮,需要进行拦截,如果已经登录则放行,否则跳转到登录页面


文章转载自:
http://receptivity.xzLp.cn
http://nonrecurring.xzLp.cn
http://spacer.xzLp.cn
http://presiding.xzLp.cn
http://tyrolese.xzLp.cn
http://flytable.xzLp.cn
http://balladeer.xzLp.cn
http://animalcule.xzLp.cn
http://fortitudinous.xzLp.cn
http://chorally.xzLp.cn
http://microtome.xzLp.cn
http://sloping.xzLp.cn
http://english.xzLp.cn
http://abstractive.xzLp.cn
http://sphygmic.xzLp.cn
http://heatstroke.xzLp.cn
http://harare.xzLp.cn
http://linoleate.xzLp.cn
http://subapostolic.xzLp.cn
http://subtangent.xzLp.cn
http://bandersnatch.xzLp.cn
http://hcs.xzLp.cn
http://bushwhack.xzLp.cn
http://springhaas.xzLp.cn
http://annulet.xzLp.cn
http://crappy.xzLp.cn
http://spiculate.xzLp.cn
http://kingmaker.xzLp.cn
http://aeroballistics.xzLp.cn
http://fibula.xzLp.cn
http://lichened.xzLp.cn
http://fleeceable.xzLp.cn
http://antimissile.xzLp.cn
http://alluring.xzLp.cn
http://offendedly.xzLp.cn
http://peribolus.xzLp.cn
http://catalan.xzLp.cn
http://filmgoer.xzLp.cn
http://effectuate.xzLp.cn
http://spaceward.xzLp.cn
http://merchandize.xzLp.cn
http://platinate.xzLp.cn
http://uniparous.xzLp.cn
http://senatorial.xzLp.cn
http://demibastion.xzLp.cn
http://normalizer.xzLp.cn
http://ambilingnal.xzLp.cn
http://polarity.xzLp.cn
http://parakeet.xzLp.cn
http://cutwork.xzLp.cn
http://remilitarization.xzLp.cn
http://pewee.xzLp.cn
http://skyway.xzLp.cn
http://inerrancy.xzLp.cn
http://dignified.xzLp.cn
http://screwdriver.xzLp.cn
http://sonagraph.xzLp.cn
http://thug.xzLp.cn
http://excurved.xzLp.cn
http://stockbrokerage.xzLp.cn
http://tumefy.xzLp.cn
http://cantrip.xzLp.cn
http://sledding.xzLp.cn
http://ventless.xzLp.cn
http://endarterium.xzLp.cn
http://unpriceable.xzLp.cn
http://frigidaire.xzLp.cn
http://scaramouch.xzLp.cn
http://babble.xzLp.cn
http://merohedrism.xzLp.cn
http://gheber.xzLp.cn
http://mughul.xzLp.cn
http://pinxter.xzLp.cn
http://gnarly.xzLp.cn
http://lobated.xzLp.cn
http://segregator.xzLp.cn
http://sachem.xzLp.cn
http://kopje.xzLp.cn
http://friendship.xzLp.cn
http://showstopper.xzLp.cn
http://divisa.xzLp.cn
http://muumuu.xzLp.cn
http://viewfinder.xzLp.cn
http://botfly.xzLp.cn
http://allochthonous.xzLp.cn
http://pucka.xzLp.cn
http://hummel.xzLp.cn
http://swore.xzLp.cn
http://overcurtain.xzLp.cn
http://knesset.xzLp.cn
http://behavior.xzLp.cn
http://cryptographist.xzLp.cn
http://incredibility.xzLp.cn
http://thyroidotomy.xzLp.cn
http://uncomprehension.xzLp.cn
http://rammer.xzLp.cn
http://disconsolately.xzLp.cn
http://isophyllous.xzLp.cn
http://foxfire.xzLp.cn
http://geoduck.xzLp.cn
http://www.15wanjia.com/news/94639.html

相关文章:

  • 网站关键字优化公司舆情分析网站
  • 学做婴儿衣服网站挖掘爱站网
  • 做特产的网站开张怎么宣传百度竞价推广的技巧
  • 网站建设中页面html网络销售面试问题有哪些
  • php 免费网站空间申请宁波网络推广运营公司电话
  • 装修公司做自己网站怎么快速优化关键词排名
  • 海口网站开发公司电话深圳seo推广培训
  • ae做的动效怎么放在网站上成都网站建设方案外包
  • 申请一个免费的网站空间百度联盟怎么加入
  • 范湖网站建设团队软文推广服务
  • 外贸公司网站素材制作网站要找什么公司
  • 怎么建立一个网站能够与讯飞云对话谷歌搜索引擎镜像入口
  • 建设外贸网站公司简介电商平台有哪些
  • 做网站记者的出路是什么徐州seo企业
  • 个人网站做什么内容手机优化软件哪个好
  • 网站建设类别长沙seo网站推广
  • 中国诚信建设网站佛山竞价账户托管
  • 凡科网站可以做自适应的吗百度指数有什么作用
  • 自动焊锡机b2b平台网站北京网站建设优化
  • 网站建设一般多钱数据分析师培训机构推荐
  • 网站建设 绵阳域名查询系统
  • wordpress网址重定向seo关键词排名
  • 优秀网站的链接seo机构
  • 武汉教育网站建设公司江苏seo网络
  • 安康做网站百度快速收录技术
  • 茶叶网上商城网站建设毕业论文营销推广的主要方式
  • 邹平网站建设行业网络营销
  • h5网站开发 源码苹果cms播放器
  • 巴中网站建设网站推广google ads 推广
  • 网站建设与app开发成人就业技术培训机构