vue-router

vue2.0 vue-router总结

三世轮回 提交于 2019-11-30 06:46:44
1. 在项目中安装: npm install vue-router --save 2. 在项目中引入: // The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue'; import Vuex from 'vuex'; import VueResource from 'vue-resource'; import App from './App'; import router from './router'; import store from './store'; // Resource Vue.use(VueResource); // vuex Vue.use(Vuex); Vue.config.productionTip = false; /* eslint-disable no-new */ new Vue({ el: '#app', router, store, template: '<App/>', components: { App } }); 3. router.js的配置 我们先来看一下 router

Vue-router error: TypeError: Cannot read property 'matched' of undefined

大城市里の小女人 提交于 2019-11-30 06:41:19
I'm trying to write my first Vuejs app. I'm using vue-cli and simple-webpack boilerplate . When I add vue-router links to my app component I get this error in console Error in render function: "TypeError: Cannot read property 'matched' of undefined" Here is my code: App.vue <template> <div> <h2>Links</h2> <ul> <router-link to='/'>Home</router-link> <router-link to='/query'>Query</router-link> <router-view></router-view> </ul> </div> </template> <script> export default {} </script> main.js import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) import routes from './routes

Best Practice for Reacting to Params Changes with Vue Router

大城市里の小女人 提交于 2019-11-30 06:26:26
问题 When using Vue Router with routes like /foo/:val you have to add a watcher to react for parameter changes. That results in somewhat annoying duplicate code in all views that have parameters in the URL. This could look like the following example: export default { // [...] created() { doSomething.call(this); }, watch: { '$route' () { doSomething.call(this); } }, } function doSomething() { // e.g. request API, assign view properties, ... } Is there any other way to overcome that? Can the

Vue.js redirection to another page

守給你的承諾、 提交于 2019-11-30 06:17:39
问题 I'd like to make a redirection in Vue.js similar to the vanilla javascript window.location.href = 'some_url' How could I achieve this in Vue.js? 回答1: If you are using vue-router , you should use router.go(path) to navigate to any particular route. The router can be accessed from within a component using this.$router . Otherwise, window.location.href = 'some url'; works fine for non single-page apps. EDIT : router.go() changed in VueJS 2.0. You can use router.push({ name: "yourroutename"}) or

实现vue服务端渲染

五迷三道 提交于 2019-11-30 06:06:10
1.为什么要使用服务端渲染 1.首屏渲染更快,用户可以更快的看到页面 2.对SEO(搜索引擎优化)更友好 2.实现一个简单的服务端渲染 首先,使用vue cli3创建一个vue工程 (vue create ssr) 然后安装相关依赖 vue-server-renderer(渲染器) express(node服务器) npm i vue-server-render express -D 相关依赖安装完成后就可以开始实现一个最简单的服务器渲染demo了,实际原理非常简单,就是在服务端(node)中创建一个Vue实例,在请求相关路由时,通过渲染器将Vue代码转成普通的html语法直接返回一个html页面。 这时候可以在根目录下新建一个文件 server/index.js 用来编辑服务端代码,目录结构如下: ├── public ├── server │ └──index.js // 新建文件,用来写相关服务端代码 └── src 在server/index.js中 主要实现三个功能: 1.启动本地服务: /* eslint-disable no-console */ const express = require('express') const Vue = require('vue') const app = express() /* * TODO: 创建Vue实例; *

Access router instance from my service

血红的双手。 提交于 2019-11-30 03:17:37
问题 I create a auth service ( src/services/auth.js ), with just functions and properties .. export default { login() { ... } ... } Inside login function, I need to redirect user router.go(redirect) How can I retrieve router instance ? Context In my src/main.js file, i create a router .. import VueRouter from 'vue-router' Vue.use(VueRouter) import route from './routes' const router = new VueRouter({ history: false, linkActiveClass: 'active' }) route(router) const App = Vue.extend(require('./App

Vue之 路由配置安装 以及跳转

喜夏-厌秋 提交于 2019-11-30 00:48:03
https://router.vuejs.org/zh/ vue路由配置: 1.安装 npm install vue-router --save / cnpm install vue-router --save 2、引入并 Vue.use(VueRouter) (main.js) import VueRouter from 'vue-router'; Vue.use(VueRouter); 3、配置路由 1、创建组件 引入组件 import Home from './components/Home.vue'; import News from './components/News.vue'; 2、定义路由 (建议复制s) const routes = [ { path: '/home', component: Home }, { path: '/news', component: News }, { path: '*', redirect: '/home' } /*默认跳转路由*/ ] 3、实例化VueRouter const router = new VueRouter({ routes // (缩写)相当于 routes: routes }) 4、挂载 new Vue({ el: '#app', router, render: h => h(App) }) 5

Can we make vue.js application without .vue extension component and webpack?

為{幸葍}努か 提交于 2019-11-30 00:25:50
Note: Can we write vue.js large application without using any compiler for code like currently i see all example use webpack now to make vue.js code compatible for browser . I want make vue.js application without webpack and without using .vue extension. Is it possible? if it is possible, can you provide a link or give sample how to use routing in that case. As we make component in .vue extension can be make component in .js extension and use application as we do in angular 1 where we can make whole app without any trans-compiler to convert the code. Can be done that in html , css , js file

vue-router简易的实现原理

强颜欢笑 提交于 2019-11-30 00:02:35
class VueRouter { constructor(options) { this.$options = options; this.routeMap = {}; // 路由响应式 this.app = new Vue({ data: { current: "/" } }); } i nit() { this.bindEvents(); //监听url变化 this.createRouteMap(this.$options); //解析路由配置 this.initComponent(); // 实现两个组件 } b indEvents() { window.addEventListener("load", this.onHashChange.bind(this)); window.addEventListener("hashchange", this.onHashChange.bind(this)); } o nHashChange() { this.app.current = window.location.hash.slice(1) || "/"; } c reateRouteMap(options) { options.routes.forEach(item => { this.routeMap[item.path] = item.component; }); } i

最全vue-router攻略

爱⌒轻易说出口 提交于 2019-11-29 23:44:57
简介 vue-router是与组件相辅相成的,学习路由必须要先了解组件;当然,前提是对vue的基础知识已经有了一定的了解。 vue组件 vue组件的核心思想,就是将开发模块化,便于项目的管理与维护;接下来,我们来看几组例子 1 组件的声明 a 全局组件声明 从上图中,我们可以看到,我们已经将对应的代码进行了模块化处理,但这个模块化处理只是最低级的,远远达不到我们的业务需求. b 局部组件声明 这里声明的组件,将只能在父模版使用,可能有小伙伴对父模版不明白,不过没关系,我们向下看. 看到这里,我不禁会想,这种组件看起来还可以,但总是少点什么,在实际生产中,用起来不怎么方便,有以下缺点: 全局定义 (Global definitions) 强制要求每个 component 中的命名不得重复 字符串模板 (String templates) 缺乏语法高亮,在 HTML 有多行的时候,需要用到丑陋的 \ 不支持 CSS (No CSS support) 意味着当 HTML 和 JavaScript 组件化时,CSS 明显被遗漏 没有构建步骤 (No build step) 限制只能使用 HTML 和 ES5 JavaScript, 而不能使用预处理器,如 Pug (formerly Jade) 和 Babel 那么我们能不能把其分离出来,在哪里用到,我们引入进来即可,就像用link