用Vue开发项目有一段时间,在实际项目中遇到一些问题,在里把问题记录下来,并附上解决方案,给遇到同样的问题的码友提供一个解决思路吧:

  • 测试部抛出问题一:在Vue1.0路由vue-router中,当点击菜单一个组件加载出来表格列表,输入查询条件查询,当在单击这个菜单后表格的数据没有重置查询条件和查询结果.

原因分析:Vue路由在页面渲染一个组件后加载后,再加载这个组件,组件不会摧毁后在重新生成这个组件,不会重新触发组件的生命周期中的方法.代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <title>learning vue</title>
</head>
<body> <div id="app-container">
<ul>
<li v-for="item in $root.list">
<a v-link="{ path: '/lang/' + item.id }">{{ item.name }}{{para}}{{params}}</a>
</li>
</ul>
<router-view :params="params"></router-view>
</div> <script src="js/vue.js"></script>
<script src="js/vue-router.js"></script> <script>
Vue.config.debug = true;
Vue.use(VueRouter);
var AppComponent = Vue.extend({
data: function(){
return {
params:111,
list: [
{ id: '10001', name: 'C#', message: 'Hello C#' },
{ id: '10002', name: 'Java', message: 'Hello Java.' },
{ id: '10003', name: 'C++', message: 'Hello C++' },
{ id: '10004', name: 'JavaScript', message: 'Hello JavaScript' }
]
};
}
});
var LangDetailComponent = Vue.extend({
template: `<div><h1>{{ model.name }}</h1><p>{{ model.message }}</p></div>`,
computed: {
model: function(){
var list = this.$root.list;
var id = this.$route.params.id;
for(var i = 0; i < list.length; i++){
if(list[i].id === id){
return list[i];
}
}
}
},
init:function () {
alert("init");
},
created:function () {
alert("created");
},
beforeCompile:function () {
alert("beforeCompile");
},
compiled:function () {
alert("compiled");
},
ready:function () {
alert("ready");
},
attached:function () {
alert("attached")
},
detached:function () {
alert("detached")
},
beforeDestroy:function () {
alert("beforeDestroy")
},
destroyed:function () {
alert("destroyed")
} }); var router = new VueRouter();
router.map({
'/lang/:id': { component: LangDetailComponent } });
router.start(AppComponent, '#app-container');
</script> </body>
</html>

执行效果:

着三个路由都是同一个组件,但点击其他的时候组件中的所有生命周期的方法都没有调用,去vue-router的api上看没有找到重新加载路由的配置配置项.

在实际开发中这个问题在两个菜单共用一个组件,设置传参来判断加载不同的数据的情况下,会出现另一个ready方法不走导致数据显示不真确.解决思路可以加监听路由地址触发ready事件.

而上面的解决方法是用v-if来重新加载组件,代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <title>learning vue</title>
</head>
<body> <div id="app-container">
<ul>
<li v-for="item in $root.list">
<a href="javascript:void(0)" @click="click(item.path)">{{ item.name }}</a>
</li>
</ul>
<router-view v-if="show"></router-view>
</div> <script src="js/vue.js"></script>
<script src="js/vue-router.js"></script> <script>
Vue.config.debug = true;
Vue.use(VueRouter);
var AppComponent = Vue.extend({
data: function(){
return {
show:true,
list: [
{ id: '10001', name: 'C#', message: 'Hello C#',path:"/lan"},
{ id: '10002', name: 'Java', message: 'Hello Java.',path:"/lan1"},
{ id: '10003', name: 'C++', message: 'Hello C++' ,path:"/lan2"},
{ id: '10004', name: 'JavaScript', message: 'Hello JavaScript',path:"/lan3" }
]
};
},
methods:{
click:function (path) {
debugger;
if(window.location.hash.replace("#!","")==path){
this.show=false;
Vue.nextTick(function () {
this.show=true;
}.bind(this))
}else{
this.$router.go({path:path});
}
}
}
});
var LangDetailComponent = Vue.extend({
template: `<div><h1>C#</h1><p>232323</p></div>`,
computed: {
model: function(){
var list = this.$root.list;
var id = this.$route.params.id;
for(var i = 0; i < list.length; i++){
if(list[i].id === id){
return list[i];
}
}
}
},
init:function () {
alert("init");
},
created:function () {
alert("created");
},
beforeCompile:function () {
alert("beforeCompile");
},
compiled:function () {
alert("compiled");
},
ready:function () {
alert("ready");
},
attached:function () {
alert("attached")
},
detached:function () {
alert("detached")
},
beforeDestroy:function () {
alert("beforeDestroy")
},
destroyed:function () {
alert("destroyed")
} });
var LangDetailComponent1 = Vue.extend({
template: `<div><h1>Java</h1><p>Hello Java.</p></div>`,
computed: {
model: function(){
var list = this.$root.list;
var id = this.$route.params.id;
for(var i = 0; i < list.length; i++){
if(list[i].id === id){
return list[i];
}
}
}
},
init:function () {
alert("init");
},
created:function () {
alert("created");
},
beforeCompile:function () {
alert("beforeCompile");
},
compiled:function () {
alert("compiled");
},
ready:function () {
alert("ready");
},
attached:function () {
alert("attached")
},
detached:function () {
alert("detached")
},
beforeDestroy:function () {
alert("beforeDestroy")
},
destroyed:function () {
alert("destroyed")
} });
var LangDetailComponent2 = Vue.extend({
template: `<div><h1>C++</h1><p>Hello C++</p></div>`,
computed: {
model: function(){
var list = this.$root.list;
var id = this.$route.params.id;
for(var i = 0; i < list.length; i++){
if(list[i].id === id){
return list[i];
}
}
}
},
init:function () {
alert("init");
},
created:function () {
alert("created");
},
beforeCompile:function () {
alert("beforeCompile");
},
compiled:function () {
alert("compiled");
},
ready:function () {
alert("ready");
},
attached:function () {
alert("attached")
},
detached:function () {
alert("detached")
},
beforeDestroy:function () {
alert("beforeDestroy")
},
destroyed:function () {
alert("destroyed")
} });
var LangDetailComponent3 = Vue.extend({
template: `<div><h1>JavaScript</h1><p>Hello JavaScript</p></div>`,
computed: {
model: function(){
var list = this.$root.list;
var id = this.$route.params.id;
for(var i = 0; i < list.length; i++){
if(list[i].id === id){
return list[i];
}
}
}
},
init:function () {
alert("init");
},
created:function () {
alert("created");
},
beforeCompile:function () {
alert("beforeCompile");
},
compiled:function () {
alert("compiled");
},
ready:function () {
alert("ready");
},
attached:function () {
alert("attached")
},
detached:function () {
alert("detached")
},
beforeDestroy:function () {
alert("beforeDestroy")
},
destroyed:function () {
alert("destroyed")
} }); var router = new VueRouter();
router.map({
'/lan': { component: LangDetailComponent },
'/lan1': { component: LangDetailComponent1 },
'/lan2': { component: LangDetailComponent2 },
'/lan3': { component: LangDetailComponent3 }
});
router.start(AppComponent, '#app-container');
</script> </body>
</html>

效果是:

这样在点相同的菜单,组件就重新加载,后面想优化每个菜单都加click,菜单很多话对页面是性能消耗,下篇想用vue能不能用事件委托绑定单击事件.暂时先写到这里.

这里在多说一句,Vue的路由是hash路由,所以要回去路由地址可以用 window.location.hash.replace("#!","")来获取.不知道hash路由的可以百度下,这里就不多说了

用Vue中遇到的问题和处理方法(一)的更多相关文章

  1. vue中使用echarts的两种方法

    在vue中使用echarts有两种方法一.第一种方法1.通过npm获取echarts npm install echarts --save 2.在vue项目中引入echarts 在 main.js 中 ...

  2. vue中push()和splice()的使用方法

    vue中push()和splice()的使用方法 push()使用 push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度.注意:1. 新元素将添加在数组的末尾. 2.此方法改变数组的长度 ...

  3. 用Vue中遇到的问题和处理方法

    用Vue开发项目有一段时间,在实际项目中遇到一些问题,在里把问题记录下来,并附上解决方案,给遇到同样的问题的码友提供一个解决思路吧: 测试部抛出问题一:在Vue1.0路由vue-router中,当点击 ...

  4. 在vue中使用sass的配置的方法

    1.安装sass的依赖包 npm install --save-dev sass-loader //sass-loader依赖于node-sass npm install --save-dev nod ...

  5. 在vue中添加sass的配置的方法

    1.安装sass的依赖包 npm install --save-dev sass-loader //sass-loader依赖于 node-sass npm install --save-dev no ...

  6. vue中如何引入全局样式或方法

    vue中我么会经常用到通用的一些全局的方法,如何左才能实现全局的复用减少代码累赘呢? 我们一般将公用的方法分装再utils.js文件中,然后再main.js主入口文件中将utils.js中的公共的方法 ...

  7. vue中常见的问题以及解决方法

    有一些问题不限于 Vue,还适应于其他类型的 SPA 项目. 1. 页面权限控制和登陆验证 页面权限控制 页面权限控制是什么意思呢? 就是一个网站有不同的角色,比如管理员和普通用户,要求不同的角色能访 ...

  8. Vue中组件通信的几种方法(Vue3的7种和Vue2的12种组件通信)

    Vue3组件通信方式: props $emit expose / ref $attrs v-model provide / inject Vuex 使用方法: props 用 props 传数据给子组 ...

  9. vue 中使用 AJAX获取数据的方法

    在VUE开发时,数据可以使用jquery和vue-resource来获取数据.在获取数据时,一定需要给一个数据初始值. 看下例: <script type="text/javascri ...

随机推荐

  1. gtest框架

    解析gtest框架运行机制   1.前言 Google test是一款开源的白盒单元测试框架,据说目前在Google内部已在几千个项目中应用了基于该框架的白盒测试. 最近的工作是在搞一个基于gtest ...

  2. [Android 性能优化系列]降低你的界面布局层次结构的一部分

    大家假设喜欢我的博客,请关注一下我的微博,请点击这里(http://weibo.com/kifile),谢谢 转载请标明出处(http://blog.csdn.net/kifile),再次感谢 原文地 ...

  3. Visual Studio 2015 & C#6.0 试用报告,持续更新。

    昨天早上看到了.net开源的消息,我是非常兴奋的,毕竟局限于Windows的.NET经常被人唾弃.VB暂且不提,C#常年被人指责跨平台性不佳,我也是无能为力.即使有Mono等第三方跨平台工程,.NET ...

  4. 《STL源代码剖析》---stl_hash_set.h阅读笔记

    STL仅仅规定接口和复杂度,对于详细实现不作要求.set大多以红黑树实现,但STL在标准规格之外提供了一个所谓的hash_set,以hash table实现.hash_set的接口,hash_tabl ...

  5. uva 11991 - Easy Problem from Rujia Liu?(STL)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142" target="_blank ...

  6. 利用Delphi监视注册表的变化

    转帖:利用Delphi监视注册表的变化 2009-12-23 11:53:51 分类: 利用Delphi监视注册表的变化       我们在编写软件的时候,常常需要把一些信息保存到系统的注册表中.如果 ...

  7. Oracle Database Instant Client 11g 32位和64位 安装包发布

    Oracle Database Instant Client 11g 32位和64 Oracle Database Instant Client 11g 11.2.0.3.0 Instant Clie ...

  8. NUnit单元测试资料汇总

    NUnit单元测试资料汇总 从安装到配置 首先到官网http://www.nunit.org/下载如下图的资料,安装NUnit-2.6.1.msi包. 然后挂在VS2010外部工具这个地方来使用,工具 ...

  9. Map和List

    Map和List 当把Map中的key-value对当成单独的集合元素来对待时,Map和Set就统一起来了. Map集合是一个关联数组,它包含两组值:一组是所有key组成的集合,因为Map集合的key ...

  10. Intellij Idea/Webstorm/Phpstorm

    Intellij Idea/Webstorm/Phpstorm 的高效快捷键   1. shift + F6可以理解为F2的豪华重量版,不但可以重命名文件名,而且可以命名函数名,函数名可以搜索引用的文 ...