vue-router

vue面试

早过忘川 提交于 2019-12-01 20:21:49
第一部分: 1、active-class是哪个组件的属性?嵌套路由怎么定义? 答:vue-router模块的router-link组件。 2、怎么定义vue-router的动态路由?怎么获取传过来的动态参数? 答:在router目录下的index.js文件中,对path属性加上/:id。 使用router对象的params.id 3、vue-router有哪几种导航钩子? 答:三种,一种是全局导航钩子:router.beforeEach(to,from,next),作用:跳转前进行判断拦截。第二种:组件内的钩子;第三种:单独路由独享组件 4、scss是什么?安装使用的步骤是?有哪几大特性? 答:预处理css,把css当前函数编写,定义变量,嵌套。 先装css-loader、node-loader、sass-loader等加载器模块,在webpack-base.config.js配置文件中加多一个拓展:extenstion,再加多一个模块:module里面test、loader 4.1、scss是什么?在vue.cli中的安装使用步骤是?有哪几大特性? 答:css的预编译。 使用步骤: 第一步:用npm 下三个loader(sass-loader、css-loader、node-sass) 第二步:在build目录找到webpack.base.config.js

webpack--vue-router

二次信任 提交于 2019-12-01 19:36:15
一、安装 cnpm install vue-router -S    如果在一个模块化工程中使用它,必须通过Vue.use()明确安装路由功能: import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter)    二、步骤 1、导入VueRouter包 import VueRouter from 'vue-router' 2、手动安装VUeRouter Vue.use(VueRouter) 3、导入组件 import account from './main/Account.vue' import goodsList from './main/GoodsList.vue' import login from './subcom/login.vue' import register from './subcom/register.vue' 4、创建路由对象 var router = new VueRouter({ routes = [ { path: '/account', component:account} ] })   5、子路由 routes:[ // account goodlist { path:'/account', component:account, children:[ {

How base option works in vue-router

心已入冬 提交于 2019-12-01 16:36:19
As par documentation of base option: The base URL of the app. For example, if the entire single page application is served under /app/, then base should use the value "/app/". But I have tried it like following, It does not seems to work: const router = new VueRouter({ base: "/app/", routes }) Demo fiddle . The base has a default value of '/' . Drawing analogy from how it is used to route: <router-link to="home">Home</router-link> or <router-link :to="{ path: '/abc'}" replace></router-link> I just omitted the /app and it works . The base doesn't need to be part of the router-link EDIT Use of

[Vue warn]: Unknown custom element: <router-view> - did you register the component correctly?

被刻印的时光 ゝ 提交于 2019-12-01 15:56:53
I use Vue 2 in CLI mode with webpack-simple. I have following files: main.js: import Vue from 'vue' import App from './App.vue' import VueRouter from 'vue-router' import Routes from './routes' Vue.use('VueRouter'); const router = new VueRouter({ routes: Routes }) new Vue({ el: '#app', render: h => h(App), router: router, }) App.vue: <template> <div> <router-view></router-view> </div> </template> <script> import Loader from './Loader.vue' export default { name: 'app', } </script> <style lang="scss"> </style> routes.js: import Game from './components/Game.vue'; import Login from './components

[Vue] vue-router-interview

时光怂恿深爱的人放手 提交于 2019-12-01 15:24:53
1.vue-router 怎么重定向页面? 路由中配置 redirect 属性 使用路由的别名来完成重定向 2.vue-router 怎么配置 404 页面? path: '*' ,放在最后一个 3.切换路由时,需要保存草稿的功能,怎么实现呢? beforeDe beforeRouteLeave 4.vue-router 路由有几种模式?说说它们的区别? hash 模式的特点 hash 表示的是地址栏 URL 中#符号(也称作为锚点), hash 虽然会出现在 URL 中, 但是不会被包含在 Http 请求中, 因此 hash 值改变不会重新加载页面.由于 hash 值变化不会引起浏览器向服务器发出请求, 而且 hash 改变会触发 hashchange 事件, 浏览器的进后退也能对其进行控制, 所以在 HTML5 之前, 基本都是使用 hash 来实现前端路由. history 模式的特点 利用了 HTML5 新增的 pushState()和 replaceState()两个 api, 通过这两个 api 完成 URL 跳转不会重新加载页面.同时 history 模式解决了 hash 模式存在的问题. hash 的传参是基于 URL 的, 如果要传递复杂的数据, 会有体积限制, 5.说说你对 router-link 的了解 vue-router 插件的其中一个组件, 用于跳转路由

(4)添加vue-router

喜欢而已 提交于 2019-12-01 11:47:46
(4)添加vue-router 1 安装vue-router cnpm install vue-router --save 2 页面准备 新建目录 /src/views/common ,此目录下面建立4个组件 404.vue 、 home.vue 、 login.vue 、 theme.vue 。每个文件只有表示该文件的一个题目,如 theme.vue 为: <template> <div> <h1>theme</h1> </div> </template> <script> export default { } </script> 3 路由配置 新建文件 /src/router/index.js ,内容如下: /** * 全站路由配置 * * 建议: * 1. 代码中路由统一使用name属性跳转(不使用path属性) */ import Vue from 'vue' import Router from 'vue-router' // 全局路由(无需嵌套上左右整体布局) const globalRoutes=[ { path:'/404', component:require('@/views/common/404.vue').default, name:'404', meta:{title:'404未找到'} }, { path:'/login', component

How to destroy a VueJS component that is being cached by <keep-alive>

这一生的挚爱 提交于 2019-12-01 11:22:32
I have a Vue component that's kept alive using Vue's element for caching purposes. However, the problem I am having right now is that once I sign out of one account and create a new account on my Vue application, the component I'm "keeping alive" is being reflected for the new user (which obviously isn't relevant for the new user). As a result, I want to destroy that component once the user signs out. What is the best way to go about this? I've managed to solve my issue in the following way. Essentially, if the user is logged in, keep the dashboard alive. Else, don't keep the dashboard alive.

Vue.js Router: Run code when component is ready

我与影子孤独终老i 提交于 2019-12-01 09:07:42
I'm working on a single-page app with Vue.js and its official router. I have a menu and a component (.vue file) per every section which I load using the router. In every component I have some code similar to this: <template> <div> <!-- MY DOM --> </div> </template> <script> export default { data () {}, methods: {}, route: { activate() {}, }, ready: function(){} } </script> I want to execute a piece of code (init a jQuery plugin) once a component has finished transitioning in. If I add my code in the ready event, it gets fired only the first time the component is loaded. If I add my code in the

vue-router creates always a new Component instance

余生长醉 提交于 2019-12-01 08:01:11
I found an issue in vue-router which triggers me a lot. Always when I switch between my routes, a new instance of the component is created. Further the old instances are not deleted and are running in background! I would expect that when I open a route, the old components will be destroyed or stop running. Is there a workaround to fix that issue? Here is a fiddle: https://jsfiddle.net/4xfa2f19/5885/ let foo = { template: '<div>Foo</div>', mounted() { console.log('Mount Foo with uid: ' + this._uid); setInterval(() => {console.log('Instance ' + this._uid + ' of Foo is running')}, 500); } }; let

8. Vue - Router

随声附和 提交于 2019-12-01 07:54:55
一、Vue Router 的使用 JavaScript: 1.创建组件:创建单页面应用需要渲染的组件 2.创建路由:创建VueRouter实例 3.映射路由:调用VueRouter实例的map方法 4.启动路由:调用VueRouter实例的start方法 HTML: 1.使用v-link指令 2.使用<router-view>标签 router.redirect: 1.应用在首次运行时右侧是一片空白,应用通常都会有一个首页,例如:Home页。 2.使用router.redirect方法将根路径重定向到/home路径: router.redirect({'/': '/home'}) router.redirect 方法用于为路由器定义全局的重定向规则,全局的重定向会 在匹配当前路径之前执行。 执行过程: 当用户点击v-link指令元素时,我们可以大致猜想一下这中间发生了什么事情: 1.vue-router首先会去查找v-link指令的路由映射 2.然后根据路由映射找到匹配的组件 3.最后将组件渲染到<router-view>标签 1. 引入Vue和Vue-Router插件 <script src="vue.js"></script> <script src="vue-router.js"></script> 2. HTML <div id="box"> </div> <!--定义模版