vue-router

vue-router 基本使用

匿名 (未验证) 提交于 2019-12-02 21:53:52
  点击之后,怎么做到正确的对应,比如,我点击home 按钮,页面中怎么就正好能显示home的内容。这就要在js 文件中配置路由。   路由中有三个基本的概念 route, routes, router。     2, routes 是一组路由,把上面的每一条路由组合起来,形成一个数组。[{home 按钮 =>home内容 }, { about按钮 => about 内容}]     3, router 是一个机制,相当于一个管理者,它来管理路由。因为routes 只是定义了一组路由,它放在哪里是静止的,当真正来了请求,怎么办? 就是当用户点击home 按钮的时候,怎么办?这时router 就起作用了,它到routes 中去查找,去找到对应的 home 内容,所以页面中就显示了 home 内容。     4,客户端中的路由,实际上就是dom 元素的显示和隐藏。当页面中显示home 内容的时候,about 中的内容全部隐藏,反之也是一样。客户端路由有两种实现方式:基于hash 和基于html5 history api.    vue-router中的路由也是基于上面的内容来实现的    在vue中实现路由还是相对简单的。因为我们页面中所有内容都是组件化的,我们只要把路径和组件对应起来就可以了,然后在页面中把组件渲染出来。   1, 页面实现(html模版中)        2, js

How to set URL query params in Vue with Vue-Router

谁说胖子不能爱 提交于 2019-12-02 21:32:30
I am trying to set query params with Vue-router when changing input fields, I don't want to navigate to some other page but just want to modify url query params on the same page, I am doing like this: this.$router.replace({ query: { q1: "q1" } }) But this also refreshes the page and sets the y position to 0, ie scrolls to the top of the page. Is this the correct way to set the URL query params or is there a better way to do it. Edited: Here is my router code: export default new Router({ mode: 'history', scrollBehavior: (to, from, savedPosition) => { if (to.hash) { return {selector: to.hash} }

Redirect to requested page after login using vue-router

拈花ヽ惹草 提交于 2019-12-02 18:07:32
In my application some routes are just accessible for authenticated users. When a unauthenticated user clicks on a link, for which he has to be signed in, he will be redirected to the login component. If the user logs in successfully, I would like to redirect him to the URL he requested before he had to log in . However, there also should be a default route , in case the user did not request another URL before he logged in. How can I achieve this using vue-router? My code without redirect after login router.beforeEach( (to, from, next) => { if(to.matched.some(record => record.meta.forVisitors)

Enclosing a router-link tag in a button in vuejs

南笙酒味 提交于 2019-12-02 17:51:43
Can I wrap or enclose a router-link tag in a button tag? When I press the button, I want it to route me to the desired page. You can use tag prop . <router-link to="/foo" tag="button">foo</router-link> @choasia 's answer is correct. Alternatively, you could wrap a button tag in a router-link tag like so: <router-link :to="{name: 'myRoute'}"> <button id="myButton" class="foo bar">Go!</button> </router-link> This is not so clean because your button will be inside a link element ( <a> ). However, the advantage is that you have a full control on your button, which may be necessary if you work with

How to VueJS router-link active style

≡放荡痞女 提交于 2019-12-02 17:37:26
My page currently has Navigation.vue component. I want to make the each navigation hover and active. The 'hover' works but 'active' doesn't. This is how Navigation.vue file looks like : <template> <div> <nav class="navbar navbar-expand-lg fixed-top row"> <router-link tag="li" class="col" class-active="active" to="/" exact>TIME</router-link> <router-link tag="li" class="col" class-active="active" to="/CNN" exact>CNN</router-link> <router-link tag="li" class="col" class-active="active" to="/TechCrunch" exact>TechCrunch</router-link> <router-link tag="li" class="col" class-active="active" to="

How to share data between components in VueJS

眉间皱痕 提交于 2019-12-02 17:33:03
I have a fairly simple VueJS app, 3 components (Login, SelectSomething, DoStuff) Login component is just a form for user and pass input while the second component needs to display some data obtained in the login progress. How can I share data from one component to the others? So that when I route to second component I still have the data obtained in the Login one? You can either use props or an event bus, where you'll be able to emit an event from a component and listen on another vm.$on('test', function (msg) { console.log(msg) }) vm.$emit('test', 'hi') // -> "hi" In Vue.js components can

vue-router使用

て烟熏妆下的殇ゞ 提交于 2019-12-02 17:05:06
Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌。 包含的功能有: 嵌套的路由/视图表 模块化的、基于组件的路由配置 路由参数、查询、通配符 基于 Vue.js 过渡系统的视图过渡效果 细粒度的导航控制 带有自动激活的 CSS class 的链接 HTML5 历史模式或 hash 模式,在 IE9 中自动降级 自定义的滚动条行为 Router实现路由跳转,子路由。 1.通过new Router创建路由实例 使用 router-link 组件来导航. 通过传入 `to` 属性指定链接. <router-link to="/A"></router-link>,如果组件中存在子路由,需要在父组件中添加<router-view></router-view>.嵌套路由实现方法:children:[{},{}] <!--index.js--> import Vue from 'vue' import Router from 'vue-router' import First from '@/components/first' import A from '@/components/A' import B from '@/components/B' import A1 from '@/components/A1' Vue

vue-router-基础

拟墨画扇 提交于 2019-12-02 16:57:18
vue-router-基础 vue 路由的mode(模式)有几种, 分别是什么?在那些环境下运行? 【 黄金 】 hash: 使用 URL hash 值来作路由。支持所有浏览器,包括不支持 HTML5 History Api 的浏览器。 #/home history: 依赖 HTML5 History API 和服务器配置。【需要后端支持】 /home abstract: 支持所有 JavaScript 运行环境,如 Node.js 服务器端。如果发现没有浏览器的 API,路由会自动强制进入这个模式。【 这个模式不常用 】 hash/history 常用于浏览器端,abstract用于服务端 路由的使用步骤 . 装 vue-router - yarn add vue-router 在src目录下创建一个router目录, 里面创建一个index.js文件 , 这个目录就是router的模块 引入第三方的依赖包, 并注册路由 import Vue from 'vue' import VueRouter from 'vue-router' Vue.use( VueRouter ) //使用vue-router这个第三方插件 注意: import这个关键字要放在整个文件的上层 创建了一个router对象实例,并且创建路由表 const routes = [ { path: '/home'

vue-router-基础

旧城冷巷雨未停 提交于 2019-12-02 16:38:34
vue-router-基础 vue 路由的mode(模式)有几种, 分别是什么?在那些环境下运行? 【 黄金 】 hash: 使用 URL hash 值来作路由。支持所有浏览器,包括不支持 HTML5 History Api 的浏览器。 #/home history: 依赖 HTML5 History API 和服务器配置。【需要后端支持】 /home abstract: 支持所有 JavaScript 运行环境,如 Node.js 服务器端。如果发现没有浏览器的 API,路由会自动强制进入这个模式。【 这个模式不常用 】 hash/history 常用于浏览器端,abstract用于服务端 路由的使用步骤 . 装 vue-router - yarn add vue-router 在src目录下创建一个router目录, 里面创建一个index.js文件 , 这个目录就是router的模块 引入第三方的依赖包, 并注册路由 import Vue from 'vue' import VueRouter from 'vue-router' Vue.use( VueRouter ) //使用vue-router这个第三方插件 注意: import这个关键字要放在整个文件的上层 创建了一个router对象实例,并且创建路由表 const routes = [ { path: '/home'

Vue.js page transition fade effect with vue-router

北战南征 提交于 2019-12-02 16:21:45
How to achieve a fade effect page transition between vue-router defined pages (components)? Wrap <router-view></router-view> with <transition name="fade"></transition> and add these styles: .fade-enter-active, .fade-leave-active { transition-property: opacity; transition-duration: .25s; } .fade-enter-active { transition-delay: .25s; } .fade-enter, .fade-leave-active { opacity: 0 } Detailed answer Assuming you have created your application with vue-cli, e.g.: vue init webpack fadetransition cd fadetransition npm install Install the router: npm i vue-router If you are not developing your app