vue-router

Vue-router

天涯浪子 提交于 2020-01-04 12:54:29
Vue Router是Vue.js官方的路由管理器,严格来说,它是一款插件,但它又和Vue.js核心深度集成,Vue Router的版本依赖于Vue.js的版本,最新的vue-router 3.0 依赖于Vue.js 2.0及以上版本。 一  安装Vue-router    1,简介   Vue Router 有两种模式,分别是HTML5 History模式和hash模式。我们知道,Vue.js是主要用于构建单页面应用用户界面的渐进式框架,所以hash是Vue推荐的主力模式,如果你要使用History模式,需要配合后端进行一些单独的设置。   Vue Router 提供的主要功能包括但不限于:     嵌套路由和视图;     基于模块化、组件化的路由配置;     路由参数、查询;     简单过渡效果。         2,安装    安装Vue Router也很简单,三种方式:script标签本地引入,CDN加速,模块化开发npm安装。 1 //标签本地引入 2 <script src="/vue.js"></script> 3 <script src="/vue-router.js"></script> 4 //vue-router依赖于vue.js,序偶一要先引入vue.js 1 //CDN加速 2 <script src="https://cdn.jsdelivr

Unable to create Vue Router instance in JavaScript

青春壹個敷衍的年華 提交于 2020-01-04 06:13:13
问题 Following the guide I arrived at the point where I need to create an instance of Vue (which seems to work). However, I also need to provide an instance of Vuew Router into the constructor of Vue, as shown below. const router = new VueRouter({ routes }); const app = new Vue({ router }).$mount('#app'); Regrettably, it produces the following error. I'm very uncertain on how to deal with it and troubleshooting seems to be rather sparesely documented when googlearching. Uncaught ReferenceError:

Access LocalStorage in Middleware - NuxtJs

纵饮孤独 提交于 2020-01-04 05:46:20
问题 Well, I'm starting with nuxt and I have following routes: /home /dashboard /login I want to protect the /dashboard, but only for users logged in with a token in localStorage . The simplest way I thought of doing this was by creating a /middleware/auth.js export default function () { if (!window.localStorage.getItem('token')) { window.location = '/login' } } and registering it in the /dashboard/index.vue component. <script> export default { middleware: 'auth', } </script> But I cannot access

Vue router2 not catching deep nested routes

假如想象 提交于 2020-01-04 03:51:29
问题 My routes has the following structure divided into several files: export const router = new VueRouter({ mode: 'hash', base: __dirname, saveScrollPosition: true, history: true, routes : Array.concat(userRoutes, siteRoutes) }) ... // user/routes.js export const userRoutes = [ { path: '/user', name: 'user_main', component: UserMain, meta: {authRequired: true}, children: Array.concat([ ... ], accountRoutes) } ] // account/routes.js export const accountRoutes = [ { path: 'account', name: 'user

Vue知识点精简汇总

浪子不回头ぞ 提交于 2020-01-04 00:59:17
一、 组件component 1. 什么是组件? 组件(Component)是 Vue.js 最强大的功能之一。组件可以扩展 HTML 元素,封装可重用的代码 组件是自定义元素(对象) 2. 定义组件的方式 方式1:先创建组件构造器,然后由组件构造器创建组件 方式2:直接创建组件 3. 组件的分类 分类:全局组件、局部组件 4. 引用模板 将组件内容放到模板<template>中并引用 5. 动态组件 <component :is="">组件 多个组件使用同一个挂载点,然后动态的在它们之间切换 <keep-alive>组件 二、 组件间数据传递 1. 父子组件 在一个组件内部定义另一个组件,称为父子组件 子组件只能在父组件内部使用 默认情况下,子组件无法访问父组件中的数据,每个组件实例的作用域是独立的 2. 组件间数据传递 (通信) 2.1 子组件访问父组件的数据 a)在调用子组件时,绑定想要获取的父组件中的数据 b)在子组件内部,使用props选项声明获取的数据,即接收来自父组件的数据 总结:父组件通过props向下传递数据给子组件 注:组件中的数据共有三种形式:data、props、computed 2.2 父组件访问子组件的数据 a)在子组件中使用vm.$emit(事件名,数据)触发一个自定义事件,事件名自定义 b)父组件在使用子组件的地方监听子组件触发的事件

redirect after axios response with vue

混江龙づ霸主 提交于 2020-01-02 10:18:21
问题 Ok, so i have the following method. Basically I am calling an API backend, posting username and password, and if those pass, in the response, the server sends me a 200 status. What I want to do, is in the response, after I get the status, run an if/else, so if the status is 200, redirect, else display an error message. every time i try to use 'this.$router.push('/any route here')', I get an error: 'homepage.vue?3ec6:72 Uncaught (in promise) TypeError: Cannot read property '$router' of

vue全家桶(vue-cli,vue-router,vue-resource,vuex)-1

♀尐吖头ヾ 提交于 2020-01-02 09:15:53
vue-cli # 全局安装 vue-cli $ npm install --global vue-cli # 创建一个基于 webpack 模板的新项目 $ vue init webpack my-project # 安装依赖 $ cd my-project $ npm install $ npm run dev  vue-router import Router from 'vue-router' Vue.use(Router) let router=new Router({ routes: [//路径和页面一一对应 { path: '/home', name: 'Home', component: Home }, { path: '/', redirect: '/home' },//重定向 ] }); //名称代替路径 <router-link class="footer-item" exact to="home">首页</router-link> //打开页面前验证 router.beforeEach((to, from, next) => { if(to.path!=="/logon"&&!store.state.userInfo.Account){ next({ path: '/logon' }) return; }else{ next(); } }) vue

vuex和vue-router全家桶

自作多情 提交于 2020-01-02 09:15:35
vuex 关键词:state,getters,actions,mutations,store。 state为状态的参数。 getters为获取器,用于过滤的方法获取参数。 mutations是改变状态参数的函数,但是不能直接被调用,需要对应的 store.commit (可以额外传参数)。 actions不是直接修改状态,而是基于mutations,可以执行异步处理 store更像一个容器,装着以上的所有函数和参数,最后需要注入到Vue的实例当中。 2.0 特性--辅助函数 辅助函数j就是可用可不用。如果你用了,它就会提高代码编写效率的。 mapState mapGetter mapMutation 举个例子,如果不用辅助函数 mapState , computed: { count () { return this.$store.state.count } } 使用辅助函数 mapState , computed: mapState([ // 映射 this.count 为 store.state.count 'count' ]) 如果不用辅助函数 mapGetter computed: { doneTodosCount () { return this.$store.getters.doneTodosCount } } 使用辅助函数 mapGetter computed: {

Vue router server configuration for history mode on nginx does not work

*爱你&永不变心* 提交于 2020-01-02 04:50:40
问题 I read the following note from vue router documentation Note : when using the history mode, the server needs to be properly configured so that a user directly visiting a deep link on your site doesn't get a 404. So, I try to configure my nginx like the following server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www/laravel/public/; index index.php index.html index.htm; server_name working.dev; location /user { rewrite ^(.+)$ /index.php last; } location /

Error in render function: “TypeError: Cannot read property of undefined” in Vue

你说的曾经没有我的故事 提交于 2020-01-02 04:08:31
问题 I am using Laravel and vue-router. <template> <div class="content__inner"> <div class="forums"> <!-- Heading --> <div class="forums__heading" :style="'border-bottom:2px solid #' + board.category.color"> <div class="lg-8 md-8 sm-12 column column__first"> <h2 class="forums__heading__title">{{ board.title }}</h2> </div> <div class="lg-1 md-1 sm-1 dtop column text-center"> <strong>Replies</strong> </div> <div class="lg-3 md-3 sm-4 column text-right"> <strong>Latest Reply</strong> </div> <div