一.Vue的插件在一个页面里的使用
- 这样子基本上没有太大的效率
- 无法重复利用,没起到封装的作用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>第三方插件</title>
</head>
<body>
<div id="app">
<h2 v-text='msg'></h2>
<h2 v-upper-text="msg"></h2>
<h2 v-lower-text="msg"></h2>
</div>
</body>
<script src="../js/vue.js"></script>
<script>
Vue.config.productionTip = false;
Vue.prototype.$now = Date.now();
Vue.prototype.$now = new Date().toTimeString();
Vue.directive('upper-text', function (el, binding) {
el.innerText = binding.value.toUpperCase();
})
Vue.showInfo = function () {
console.log("展示一下信息");
}
const vm = new Vue({
el: "#app",
data() {
return {
msg: "helloWorld",
}
},
})
</script>
</html>
二.封装plugin的JS页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>第三方插件</title>
</head>
<body>
<div id="app">
<h2 v-text='msg'></h2>
<h2 v-upper-text="msg"></h2>
<h2 v-lower-text="msg"></h2>
<h2>此时的时间的毫秒数是:{{$now1}}</h2>
<h2>此时的时间是:{{$now2}}</h2>
<h2>此时的时间是:{{$now3}}</h2>
<h2>调用[2,8)之间的随机整数:{{$random(2,8)}}</h2>
</div>
</body>
<script src="../js/vue.js"></script>
<script src="./atguigu.js"></script>
<script>
Vue.config.productionTip = false;
Vue.use(atguigu);
console.log(Vue.projectName);
console.log(Vue.version);
Vue.showInfo()
const vm = new Vue({
el: "#app",
data() {
return {
msg: "helloWorld",
}
},
})
</script>
</html>
const atguigu={};
atguigu.install=function(Vue,options){
Vue.directive('upper-text',function(el,binding){
el.innerText=binding.value.toUpperCase();
})
Vue.directive('lower-text',function(el,binding){
el.innerText=binding.value.toLowerCase();
})
Vue.projectName="superMall";
Vue.version="v1.0";
Vue.showInfo=function(){
console.log("展示一些信息");
}
Vue.prototype.$now1=Date.now();
Vue.prototype.$now2=new Date().toLocaleString();
Vue.prototype.$now3=new Date().toTimeString();
Vue.prototype.$random=function(min,max){
return Math.floor(Math.random()*(max-min))+min
}
}
