最近都在作vue相关的项目,在公司推行先后端分离,重构之前的项目,真的好忙,一个项目接着一个,爬不完的坑,不说了,说多了都是眼泪。开始正文了!!!vue
解决办法:ios
使用Vue组件切换过程钩子activated(keep-alive组件激活时调用),而不是挂载钩子mounted:vuex
<script>
export default {
activated: function() {
this.getData()
}
}
</script>
复制代码
参考网址关于keep-alive组件的钩子:https://cn.vuejs.org/v2/api/#activatedaxios
exportAllData(val){
//所有导出
if(!val){
this.exportFile(this.exportAllType);
}
},
exportFile(exportType){
let url='';//接口地址
this.$axios.get(url,{responseType: 'arraybuffer'}).then(res => {
this.download(res.data,exportType);
},res => {
this.$Message.error('导出失败');
});
},
download (data,exportType) {
if (!data) {
return
}
let exportGs='';
if(exportType==='excel'){
exportGs='application/vnd.ms-excel';
}else if(exportType==='xml'){
exportGs='text/xml';
}
let url = window.URL.createObjectURL(new Blob([data],{type: exportGs}));
let link = document.createElement('a')
link.style.display = 'none'
link.href = url;
link.setAttribute('download', '文件');
document.body.appendChild(link)
link.click();
}
复制代码
v-bind="$attrs" v-on="$listeners"
复制代码
Vue 2.4 版本提供了这种方法,将父组件中不被认为 props特性绑定的属性传入子组件中,一般配合 interitAttrs 选项一块儿使用。之因此要提到这两个属性,是由于二者的出现使得组件之间跨组件的通讯在不依赖 vuex 和事件总线的状况下变得简洁,业务清晰。后端
<template>
<div>
<span>{{child1}}<span>
<!-- C组件中能直接触发test的缘由在于 B组件调用C组件时 使用 v-on 绑定了$listeners 属性 -->
<!-- 经过v-bind 绑定$attrs属性,C组件能够直接获取到A组件中传递下来的props(除了B组件中props声明的) -->
<c v-bind="$attrs" v-on="$listeners"></c>
</div>
</template>
<script>
import c from './c.vue';
export default {
props: ['child1'],
data () {
return {};
},
inheritAttrs: false,
components: { c },
mounted () {
this.$emit('test1');
}
};
</script>
复制代码
C组件api
<template>
<div>
<span>{{child2}}<span>
</div>
</template>
<script>
export default {
props: [child2'], data () { return {}; }, inheritAttrs: false, mounted () { this.$emit('test2'); } }; </script> 复制代码
A组件:bash
<template>
<div id="app">
<b :child1="child1" :child2="child2" @test1="test1" @test2="test2"></b>
</div>
</template>
<script>
import b from './b.vue';
export default {
data () {
return {
child1:'hello child1',
child2:'hello child2'
};
},
components: { b },
methods: {
test1 () {
console.log('test1');
},
test2 () {
console.log('test2');
}
}
};
</script>
复制代码
记录平常开发中用到的一些知识点,权当一次总结吧。app