1. 新建utils.js
2. utils.js里创建windows字典数据
// utils.js里创建windows字典数据
window.DICTIONARY1 = {};
DICTIONARY1['man'] = '男';
DICTIONARY1['woman'] = '女';
// utils.js里挂载全局的方法
window.printStr = function(str) {
return str;
};
// utils.js里挂载在vue的原型上
Vue.prototype.printStr1 = window.printStr;
3.组件里使用
<template> </template>
<script>
export default {
mounted() {
// 使用
let sex = DICTIONARY1['man'];
console.log(sex);
sex = DICTIONARY1['woman'];
console.log(sex);
//使用
let str = printStr('你好');
console.log(str);
str = this.printStr1('VUE你好');
console.log(str);
// 输出 VUE你好
},
};
</script>
<style></style>
结果: