vue-router

Keep-alive on a single route instaead of all routes in router-view

余生颓废 提交于 2019-12-03 15:54:29
问题 if keep-alive is specified to router-view as below <keep-alive> <router-view></router-view> </keep-alive> then all routes are effectively cached and reloaded when that route is revisited. I'd like to be able to specify a keep-alive option on individual routes. With many routes and only 1 or 2 that need to be kept alive wihout re-rendering caching all routes is useless is there any method of doing so or any workaround available 回答1: https://jsfiddle.net/Linusborg/L613xva0/4/ New in Vue version

vue中mode hash 和 history的区别

六眼飞鱼酱① 提交于 2019-12-03 15:31:37
对于 Vue 这类渐进式前端开发框架,为了构建 SPA(单页面应用),需要引入前端路由系统,这也就是 Vue-Router 存在的意义。前端路由的核心,就在于 —— 改变视图的同时不会向后端发出请求。 为了达到这一目的,浏览器当前提供了以下两种支持: hash —— 即地址栏 URL 中的 # 符号(此 hash 不是密码学里的散列运算)。 比如这个 URL:http://www.abc.com/#/hello,hash 的值为 #/hello。它的特点在于:hash 虽然出现在 URL 中,但不会被包括在 HTTP 请求中,对后端完全没有影响,因此改变 hash 不会重新加载页面。 history —— 利用了 HTML5 History Interface 中新增的 pushState() 和 replaceState() 方法。(需要特定浏览器支持) 这两个方法应用于浏览器的历史记录栈,在当前已有的 back、forward、go 的基础之上,它们提供了对历史记录进行修改的功能。只是当它们执行修改时,虽然改变了当前的 URL,但浏览器不会立即向后端发送请求。 因此可以说,hash 模式和 history 模式都属于浏览器自身的特性,Vue-Router 只是利用了这两个特性(通过调用浏览器提供的接口)来实现前端路由。 使用场景 一般场景下,hash 和 history 都可以

Dynamically inserting a CSS class to navbar based on route

梦想的初衷 提交于 2019-12-03 14:47:40
Vue 2 and Vue-Router 2. I'm trying to change the color of my app's navbar based on which route is visited. Here's what I have: main.js: import App from "../components/App.vue" const app = new Vue({ router: Router, template: '<app></app>', components: { App } }).$mount('#app') App.vue: <template> <div> <div class="navbar" v-on:class="{ colorNav: 'color-nav' }"></div> <router-view></router-view> </div> </template> <script> export default { data() { return { colorNav: false } } } </script> With this setting, since colorNav property is false, the color-nav class is not added to the navbar. Working

Vue组件化和路由

别等时光非礼了梦想. 提交于 2019-12-03 14:46:04
组件化 组件化是 vue 的核心思想,它能提高开发效率,方便 重复 使用,简化调试步骤, 提升整个项目的可维护性 ,便于多人协同开发 组件通信 父组件 => 子组件: 属性 props 特性 $attrs 引用 refs // childprops: { msg: String } // parent<HelloWorld msg="Welcome to Your Vue.js App"/> // child :并未在 props 中声明 foo <p> {{$attrs.foo}} </p> // parent<HelloWorld foo="foo"/> // parent<HelloWorld ref="hw"/> mounted() { this.$refs.hw.xx = 'xxx' } 子元素 $children // parentthis.$children[0].xx = 'xxx' 子元素不保证顺序 子组件 => 父组件:自定义事件 // child this.$emit('add', good) // parent <Cart @add = "cartAdd($event)" ></Cart> 兄弟组件:通过共同祖辈组件 通过共同的祖辈组件搭桥, $parent 或 $root 。 // brother1 this . $parent . $on ( 'foo'

Vue.js routes serving from subdirectory

那年仲夏 提交于 2019-12-03 12:49:46
I would like to serve my Vue.js app from a subdirectory on a staging server. For example: http://url.com/subdir/app/ Right now if I do this and set up the build config assetsPublicPath to serve from that folder, all the assets are served fine but my app does not get routed correctly. The "home" page gets routed to the 'catch-all', and any further routes simply show the normal white-page 404. Here is my router: export default new Router({ mode: 'history', routes: [ { path: '/', component: ContentView, children: [ { path: '/', name: 'DataTable', component: DataTable, meta: { requiresAuth: true }

Component `mounted` fires twice on page load

对着背影说爱祢 提交于 2019-12-03 12:45:02
I have a very weird error where on page load a components mounted and beforeMount fire/run twice? Each of my components represents a page, so when I load the page on mywebsite.com/contact the Contact.vue functions mounted and beforeMount fire/run twice but if I load the page on mywebsite.com/foo the Contact.vue functions mounted and beforeMount fire/run once (which is what I think? should happen). Any idea why these functions would execute twice? I have a bit of finicky setup but it work nicely for dynamic templates. router/index.js : const router = new Router({ routes: [ { path: (window

Vue-cli 项目设置每个页面标题

為{幸葍}努か 提交于 2019-12-03 10:26:05
页面标题 在vue-router页面配置中添加meta的title信息,配合 vue-router 的 beforeEach 注册一个前置守卫用户获取到页面配置的title const title = '移动端'; export default function getPageTitle (pageTitle) { if (pageTitle) { return `${pageTitle} - ${title}` } return `${title}` } router.beforeEach((to, from, next) => { // set page title document.title = getPageTitle(to.meta.title) } router { path: '/annunciate', name: 'annunciate', component: annunciate, meta:{ title: '通告广场' } }, 来源: https://www.cnblogs.com/haonanZhang/p/11791095.html

Vue 全站缓存之 vue-router-then :前后页数据传递

心不动则不痛 提交于 2019-12-03 09:53:48
本文转载于: 猿2048 网站➮ https://www.mk2048.com/blog/blog.php?id=ia2jija01j 连续预告了好几天,总算写到这篇了。 系列篇1: Vue 全站缓存之 keep-alive : 动态移除缓存 系列篇2: Vue 全站缓存二:如何设计全站缓存 本篇为系列篇3:Vue 全站缓存之 vue-router-then :前后页数据传递 系列篇4: Vue 全站缓存之 vue-router-then :实现原理 前言 在前文 Vue 全站缓存之 keep-alive : 动态移除缓存 和 Vue 全站缓存二:如何设计全站缓存 中,我们实现了全站缓存的基础框架,在页面之间后退或同层页面之间跳转时可以复用缓存,减少了请求频率,提升了用户体验,最赞的是,开发逻辑理论上会简单直观很多。 出现了一个新需求 在父子组件之间,数据可以通过 Prop 和 $emit 事件来互相传递数据。 在本系列里,我们研究的主题是前后页的组件缓存与复用,因为使用了 keep-alive 的缓存特性,虽然前一页在界面上已经被移出,在系统里,前后页组件对象仍然是同时存在的。 于是,一个新的需求自然而然的出现了,就是 如何在前后页组件之间传递数据 。 store 模式和 vuex 前文也曾说过,为了在不同页面之间传递或共用数据,目前主流的方案是使用sotre模式或引入 vuex

webpack 引入 CDN 加速

末鹿安然 提交于 2019-12-03 08:48:39
webpack 引入 cdn 加速,用比较流行的 vue + webpack 来说明好了 1、在SPA模版入口页面,项目根目录下的 index.html 中引入 CDN <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>vue + webpack + cdn</title> </head> <body> <div id="app"></div> <script src="https://cdn.bootcss.com/vue/2.5.2/vue.min.js"></script> <script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js"></script> <script src="https://cdn.bootcss.com/vuex/3.0.1/vuex.min.js"></script> </body> </html> 2、在配置文件 build/webpack.base.conf.js 中添加 module.exports = { ... externals: { 'vue':

How to watch on Route changes with Nuxt and asyncData

寵の児 提交于 2019-12-03 08:25:27
Hi everybody i'm trying to watch on route changes in my nuxt js app. Here my middleware: export default function ({ route }) { return route; but i don't know what to write here } index.vue File middleware: [routeReact] i'm trying to write this: app.context.route = route but it says to me that app.context doesn't exist Here's the point of my question i'm trying to update my data that gets from my api with axios on page if route changing like this this the page i'm clicking link to next page : but when i'm route to next page, nothing happens all data is the same: here my asyncData code: