1)修改默认的输出目录dist为staticweb,在vue.config.js中:
module.exports = defineConfig({ outputDir: 'staticweb' })
2)判断是不是手机:
if (/(iPhone|iPod|iOS|Android|Windows Phone)/i.test(navigator.userAgent)) { alert("手机"); }else{ alert("不是手机"); }
3) 在 Vue 2 中,提示找不到 <router-view> 组件:
需要使用 Vue.use() 来注册 vue-router 插件
import Vue from 'vue' import App from './App.vue' import VueRouter from 'vue-router' import LxmMenu from './views/LxmMenu' import LxmNews from './views/LxmNews' Vue.config.productionTip = false const router = new VueRouter({ mode: 'history', routes:[ { path:'/', name:'index' }, { path:'/news', name:'news', component:LxmNews } ] }) Vue.use(VueRouter) Vue.component('lxm-menu',LxmMenu) new Vue({ render: h => h(App), router }).$mount('#app')
4) @表示的是src的绝对路径
5) vue-router提供了一个全局组件 router-link(取代a标签),能高亮,默认就会提供高亮类名,比如:
<router-link to="/news">新闻</router-link>
高亮类名:router-link-active 模糊匹配,用的比较多;
高亮类名:router-link-exact-active 精确匹配;
也可以自定义,比如:
const router = new VueRouter({ linkActiveClass:'act', linkExactActiveClass:'exact', })
6) 查询参数传参,/search?key=test
在script中可以下面方式获取得到参数值
this.$route.query.参数名
7)动态路由传参,比如:/search/:words,接收:
$route.params.参数名
8)重定向,问题:url默认是/路径,未匹配到组件时,出现空白
语法:{path:匹配路径,redirect:重定向到的路径}
9)Vue路由,404,当路径找不到匹配时,给个提示页面。需要配在路由最后面,语法path:"*"
10) 编程式导航 - 基本跳转,path路径跳转,这里比7)的route多了个r:
this.$router.push('路由路径') this.$router.push({path:'路由路径'}) this.$router.push({name:'路由名'})
用路由名的是必须在路由那里先定义了路由的名字,比如:
{ path:'/news/:key?', name:'news', component:LxmNews }
如果是query传参的话,且是多参数的话,可以这么写:
this.$router.push({ path:'/路径', query:{ 参数名1:'参数值1', 参数名2:'参数值2' } })
11) 动态参数的写法: `传递值:${this.$route.params.key}`
12)返回上一页:this.$router.back();
13)组件缓存keep-alive,路由跳转后,组件被销毁了,返回回来组件又被重建了,所以数据重新被加载了。
keep-alive有3个属性:include ,组件名数组,只有匹配的组件才会被缓存;exclude,任何匹配的的组件都不会被缓存;