-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVue 使用Use、prototype Object.defineProperties() 自定义全局插件
74 lines (65 loc) · 1.89 KB
/
Vue 使用Use、prototype Object.defineProperties() 自定义全局插件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
Vue 使用Use、prototype Object.defineProperties() 自定义全局插件
Vue 全局属性绑定
使用Object.defineProperties()模拟实现Vue的绑定原理
Object.defineProperty
-------------------------------------------------
main.js 对应全局 index.html
import Vue from 'vue'
import App from './App'
import router from './router'
import api from './http'
import i18n from './i18n'
import store from './store'
import global from '@/utils/global'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'font-awesome/css/font-awesome.min.css'
import '@/assets/iconfont/iconfont.css'
Vue.use(ElementUI)
Vue.use(api)
Vue.prototype.global = global
new Vue({
el: '#app',
i18n,
router,
store,
render: h => h(App)
});
-----------------------------------------------------------------
主要的 index.html 在目录 src外面,根目录下
可以引入全局的 js css 文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>kitty-ui</title>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
----------------------------------------
这个函数接受三个参数,一个参数是obj,表示要定义属性的对象,
一个参数是prop,是要定义或者更改的属性名字,
另外是descriptor,描述符,来定义属性的具体描述。
Object.defineProperty(obj, prop, descriptor)
// 导入所有接口
import api from './api'
const install = Vue => {
if (install.installed)
return;
install.installed = true;
Object.defineProperties(Vue.prototype, {
// 注意,此处挂载在 Vue 原型的 $api 对象上
$api: {
get() {
return api
}
}
})
}
export default install
可以这样调用
this.$api.user.findUser('黎明')