|
步骤
1
:
简单例子
步骤
2
:
带参数的自定义指令
步骤
3
:
钩子函数
像 v-if, v-bind, v-on 等等前面学习的指令,都是 vus.js 自带的指令,那么除了这些指令,开发者还可以开发效自定义的指令,比如 v-xart 来做一些效果。 如效果所示, 一旦使用了 v-xart 这个指令,那么文字颜色就会变成分红,后面也会自动加上 (x-art) 自定义指令的方式: 1. 使用Vue.directive 来自定义 2. 第一个参数就是 指令名称 xart 3. el 表示当前的html dom对象 4. 在方法体内就可以通过 innerHTML style.color 等方式操控当前元素了 Vue.directive('xart', function (el) { el.innerHTML = el.innerHTML + ' ( x-art ) ' el.style.color = 'pink' })
");
window.frames["iframe_show7825"].document.write(decodeHtml($("textarea#stepcodeTextarea7825").val()));
window.frames["iframe_show7825"].document.close();
$(window.frames["iframe_show7825"]).load(function(){
$("#iframe_show7825").height($("#iframe_show7825").contents().find("body").height()+showittryitheight);
});
$("#iframe_show7825").height($("#iframe_show7825").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<script src="http://127.0.0.1/study/vue.min.js"></script>
<div id="div1">
<div v-xart> 好好学习,天天向上 </div>
</div>
<script>
Vue.directive('xart', function (el) {
el.innerHTML = el.innerHTML + ' ( x-art ) '
el.style.color = 'pink'
})
new Vue({
el: '#div1'
})
</script>
<script src="http://127.0.0.1/study/vue.min.js"></script>
<div id="div1">
<div v-xart> 好好学习,天天向上 </div>
</div>
<script>
Vue.directive('xart', function (el) {
el.innerHTML = el.innerHTML + ' ( x-art ) '
el.style.color = 'pink'
})
new Vue({
el: '#div1'
})
</script>
");
window.frames["iframe7825"].document.write(decodeHtml(code7825));
window.frames["iframe7825"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe7825"]).load(function(){
$("#iframe7825").height($("#iframe7825").contents().find("body").height()+showittryitheight);
});
$("#iframe7825").height($("#iframe7825").contents().find("body").height()+showittryitheight);
alreadyWriteCode7825 = code7825;
$("#rendering7825").hide();
$("#rendered7825").show();
}
var tRereshRetry2DemoPanel7825 = setInterval(rereshRetry2DemoPanel7825,1000);
var binded7825 = false;
$("textarea#stepcodeTextarea7825").keyup(function(){
if(!binded7825){
$(window).bind('beforeunload',function(){
binded7825 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code7825 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code7825!=newCode){
// window.frames["iframe7825"].document.write("
");
// window.frames["iframe7825"].document.write(decodeHtml($("textarea#stepcodeTextarea7825").val()));
// window.frames["iframe7825"].document.close();
// $(window.frames["iframe7825"]).load(function(){
// $("#iframe7825").height($("#iframe7825").contents().find("body").height()+showittryitheight);
// });
// code7825 = newCode;
// }
});
$(".tryButton7825").click(function(){
$("#tryDiv7825").show();
$("#stepcodeTextarea7825").focus();
$("#stepcodeTextarea7825").height(200);
$("#iframe7825").height(0);
window.frames["iframe7825"].document.write("
");
window.frames["iframe7825"].document.write(decodeHtml($("textarea#stepcodeTextarea7825").val()));
window.frames["iframe7825"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe7825"]).load(function(){
$("#iframe7825").height($("#iframe7825").contents().find("body").height()+showittryitheight);
});
$("#iframe7825").height($("#iframe7825").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor7825.focus();
editor7825.setSize(null, "250");
$("#rendering7825").hide();
$("#rendered7825").hide();
});
var mixedMode = {
name: "htmlmixed",
scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
mode: null},
{matches: /(text|application)\/(x-)?vb(a|script)/i,
mode: "vbscript"}]
};
var editor7825 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7825"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode:"text/html",
theme:"eclipse",
selectionPointer: true,
lineWrapping: true,
extraKeys: {
"Alt-/": "autocomplete",
"Ctrl-F": "findPersistent",
"F8": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
}
}
});
editor7825.on("change",function(doc){
if(!binded7825){
$(window).bind('beforeunload',function(){
binded7825 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code7825 = newCode;
$("textarea#stepcodeTextarea7825").val(newCode);
if(alreadyWriteCode7825!=code7825){
lastModifedTime7825 = new Date().getTime();
$("#rendering7825").show();
$("#rendered7825").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor7825 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor7825.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv7825").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
上面例子是不带参数的,现在带上参数 binding.value 就是指 v-xart=“xxx” 这个xxx。 此时xxx是一个json对象,所以就可以通过.text, .color取出对应的值出来。 Vue.directive('xart', function (el,binding) { el.innerHTML = el.innerHTML + '( ' + binding.value.text + ' )' el.style.color = binding.value.color })
视图上用就传递个json 对象进去 <div v-xart="{color:'red',text:'best learning video'}"> 好好学习,天天向上 </div>
当然也可以传递个简单的 <div v-xart="blue"> 好好学习,天天向上 </div>
那么在函数里就直接调用 binding.value 就好了
");
window.frames["iframe_show7827"].document.write(decodeHtml($("textarea#stepcodeTextarea7827").val()));
window.frames["iframe_show7827"].document.close();
$(window.frames["iframe_show7827"]).load(function(){
$("#iframe_show7827").height($("#iframe_show7827").contents().find("body").height()+showittryitheight);
});
$("#iframe_show7827").height($("#iframe_show7827").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<script src="http://127.0.0.1/study/vue.min.js"></script>
<div id="div1">
<div v-xart="{color:'red',text:'best learning video'}"> 好好学习,天天向上 </div>
</div>
<script>
Vue.directive('xart', function (el,binding) {
el.innerHTML = el.innerHTML + '( ' + binding.value.text + ' )'
el.style.color = binding.value.color
})
new Vue({
el: '#div1'
})
</script>
<script src="http://127.0.0.1/study/vue.min.js"></script>
<div id="div1">
<div v-xart="{color:'red',text:'best learning video'}"> 好好学习,天天向上 </div>
</div>
<script>
Vue.directive('xart', function (el,binding) {
el.innerHTML = el.innerHTML + '( ' + binding.value.text + ' )'
el.style.color = binding.value.color
})
new Vue({
el: '#div1'
})
</script>
");
window.frames["iframe7827"].document.write(decodeHtml(code7827));
window.frames["iframe7827"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe7827"]).load(function(){
$("#iframe7827").height($("#iframe7827").contents().find("body").height()+showittryitheight);
});
$("#iframe7827").height($("#iframe7827").contents().find("body").height()+showittryitheight);
alreadyWriteCode7827 = code7827;
$("#rendering7827").hide();
$("#rendered7827").show();
}
var tRereshRetry2DemoPanel7827 = setInterval(rereshRetry2DemoPanel7827,1000);
var binded7827 = false;
$("textarea#stepcodeTextarea7827").keyup(function(){
if(!binded7827){
$(window).bind('beforeunload',function(){
binded7827 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code7827 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code7827!=newCode){
// window.frames["iframe7827"].document.write("
");
// window.frames["iframe7827"].document.write(decodeHtml($("textarea#stepcodeTextarea7827").val()));
// window.frames["iframe7827"].document.close();
// $(window.frames["iframe7827"]).load(function(){
// $("#iframe7827").height($("#iframe7827").contents().find("body").height()+showittryitheight);
// });
// code7827 = newCode;
// }
});
$(".tryButton7827").click(function(){
$("#tryDiv7827").show();
$("#stepcodeTextarea7827").focus();
$("#stepcodeTextarea7827").height(200);
$("#iframe7827").height(0);
window.frames["iframe7827"].document.write("
");
window.frames["iframe7827"].document.write(decodeHtml($("textarea#stepcodeTextarea7827").val()));
window.frames["iframe7827"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe7827"]).load(function(){
$("#iframe7827").height($("#iframe7827").contents().find("body").height()+showittryitheight);
});
$("#iframe7827").height($("#iframe7827").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor7827.focus();
editor7827.setSize(null, "250");
$("#rendering7827").hide();
$("#rendered7827").hide();
});
var mixedMode = {
name: "htmlmixed",
scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
mode: null},
{matches: /(text|application)\/(x-)?vb(a|script)/i,
mode: "vbscript"}]
};
var editor7827 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7827"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode:"text/html",
theme:"eclipse",
selectionPointer: true,
lineWrapping: true,
extraKeys: {
"Alt-/": "autocomplete",
"Ctrl-F": "findPersistent",
"F8": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
}
}
});
editor7827.on("change",function(doc){
if(!binded7827){
$(window).bind('beforeunload',function(){
binded7827 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code7827 = newCode;
$("textarea#stepcodeTextarea7827").val(newCode);
if(alreadyWriteCode7827!=code7827){
lastModifedTime7827 = new Date().getTime();
$("#rendering7827").show();
$("#rendered7827").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor7827 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor7827.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv7827").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
钩子函数是什么意思? 由叫做回调函数,或者事件响应函数。 指的是,一个指令在创建过程中,经历不同生命周期的时候,vue.js 框架调用的函数。 事件常见的有如下几种: bind:只调用一次,指令第一次绑定到元素时调用。在这里可以进行一次性的初始化设置。 update:所在组件的 VNode 更新时调用,但是可能发生在其子 VNode 更新之前。指令的值可能发生了改变,也可能没有。但是你可以通过比较更新前后的值来忽略不必要的模板更新 (详细的钩子函数参数见下)。 unbind:只调用一次,指令与元素解绑时调用。
以bind为例,可以传递主要是用到binding里的属性 name:指令名,不包括 v- 前缀。 value:指令的绑定值,本例就是hello vue.js oldValue:指令绑定的前一个值,仅在 update 和 componentUpdated 钩子中可用。无论值是否改变都可用。 expression:字符串形式的指令表达式。本例就是 message arg:传给指令的参数,本例就是hello modifiers:一个包含修饰符的对象。本例就是 .a .b 这样拿到这些自定义指令的各项参数,那么在函数体里就方便做各种自定义功能了 vnode是虚拟节点,里面具体什么意思,我也不太懂。。。好像也用不到,pass 吧~
");
window.frames["iframe_show7826"].document.write(decodeHtml($("textarea#stepcodeTextarea7826").val()));
window.frames["iframe_show7826"].document.close();
$(window.frames["iframe_show7826"]).load(function(){
$("#iframe_show7826").height($("#iframe_show7826").contents().find("body").height()+showittryitheight);
});
$("#iframe_show7826").height($("#iframe_show7826").contents().find("body").height()+showittryitheight);
setTimeout(function(){
},500);
});
<script src="http://127.0.0.1/study/vue.min.js"></script>
<div id="div1">
<div v-xart:hello.a.b="message"> </div>
</div>
<script>
Vue.directive('xart', {
bind: function (el, binding, vnode) {
var s = JSON.stringify
el.innerHTML =
'name: ' + s(binding.name) + '<br>' +
'value: ' + s(binding.value) + '<br>' +
'expression: ' + s(binding.expression) + '<br>' +
'argument: ' + s(binding.arg) + '<br>' +
'modifiers: ' + s(binding.modifiers) + '<br>' +
'vnode keys: ' + Object.keys(vnode).join(', ')
},
update: function (newValue, oldValue) {
// 值更新时的工作
// 也会以初始值为参数调用一次
},
unbind: function () {
// 清理工作
// 例如,删除 bind() 添加的事件监听器
}
})
new Vue({
el: '#div1',
data:{
message:'hello vue.js'
}
})
</script>
<script src="http://127.0.0.1/study/vue.min.js"></script>
<div id="div1">
<div v-xart:hello.a.b="message"> </div>
</div>
<script>
Vue.directive('xart', {
bind: function (el, binding, vnode) {
var s = JSON.stringify
el.innerHTML =
'name: ' + s(binding.name) + '<br>' +
'value: ' + s(binding.value) + '<br>' +
'expression: ' + s(binding.expression) + '<br>' +
'argument: ' + s(binding.arg) + '<br>' +
'modifiers: ' + s(binding.modifiers) + '<br>' +
'vnode keys: ' + Object.keys(vnode).join(', ')
},
update: function (newValue, oldValue) {
// 值更新时的工作
// 也会以初始值为参数调用一次
},
unbind: function () {
// 清理工作
// 例如,删除 bind() 添加的事件监听器
}
})
new Vue({
el: '#div1',
data:{
message:'hello vue.js'
}
})
</script>
");
window.frames["iframe7826"].document.write(decodeHtml(code7826));
window.frames["iframe7826"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe7826"]).load(function(){
$("#iframe7826").height($("#iframe7826").contents().find("body").height()+showittryitheight);
});
$("#iframe7826").height($("#iframe7826").contents().find("body").height()+showittryitheight);
alreadyWriteCode7826 = code7826;
$("#rendering7826").hide();
$("#rendered7826").show();
}
var tRereshRetry2DemoPanel7826 = setInterval(rereshRetry2DemoPanel7826,1000);
var binded7826 = false;
$("textarea#stepcodeTextarea7826").keyup(function(){
if(!binded7826){
$(window).bind('beforeunload',function(){
binded7826 = true;
return "xxxx";
});
}
var newCode = $(this).val()
code7826 = newCode;
/*below code is replaced by function rereshRetry2DemoPanel()*/
// if(code7826!=newCode){
// window.frames["iframe7826"].document.write("
");
// window.frames["iframe7826"].document.write(decodeHtml($("textarea#stepcodeTextarea7826").val()));
// window.frames["iframe7826"].document.close();
// $(window.frames["iframe7826"]).load(function(){
// $("#iframe7826").height($("#iframe7826").contents().find("body").height()+showittryitheight);
// });
// code7826 = newCode;
// }
});
$(".tryButton7826").click(function(){
$("#tryDiv7826").show();
$("#stepcodeTextarea7826").focus();
$("#stepcodeTextarea7826").height(200);
$("#iframe7826").height(0);
window.frames["iframe7826"].document.write("
");
window.frames["iframe7826"].document.write(decodeHtml($("textarea#stepcodeTextarea7826").val()));
window.frames["iframe7826"].document.close();
//load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式
$(window.frames["iframe7826"]).load(function(){
$("#iframe7826").height($("#iframe7826").contents().find("body").height()+showittryitheight);
});
$("#iframe7826").height($("#iframe7826").contents().find("body").height()+showittryitheight);
this.scrollIntoView(true);
editor7826.focus();
editor7826.setSize(null, "250");
$("#rendering7826").hide();
$("#rendered7826").hide();
});
var mixedMode = {
name: "htmlmixed",
scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i,
mode: null},
{matches: /(text|application)\/(x-)?vb(a|script)/i,
mode: "vbscript"}]
};
var editor7826 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea7826"), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode:"text/html",
theme:"eclipse",
selectionPointer: true,
lineWrapping: true,
extraKeys: {
"Alt-/": "autocomplete",
"Ctrl-F": "findPersistent",
"F8": function(cm) {
cm.setOption("fullScreen", !cm.getOption("fullScreen"));
},
"Esc": function(cm) {
if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
}
}
});
editor7826.on("change",function(doc){
if(!binded7826){
$(window).bind('beforeunload',function(){
binded7826 = true;
return "xxxx";
});
}
var newCode = doc.getValue();
code7826 = newCode;
$("textarea#stepcodeTextarea7826").val(newCode);
if(alreadyWriteCode7826!=code7826){
lastModifedTime7826 = new Date().getTime();
$("#rendering7826").show();
$("#rendered7826").hide();
}
// alert(doc.getValue());
});
$(".CodeMirror").addClass("form-control");
// var editor7826 = CodeMirror.fromTextArea(, {
// lineNumbers: true,
// styleActiveLine: true,
// matchBrackets: true,
// theme:"eclipse",
// });
editor7826.on("change",function(doc){
// alert(doc.getValue());
});
$("#tryDiv7826").hide();
});
$("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");
1. 双击选中单词
2. 三击选中整行
3. CTRL+F 查找
4. F8 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
代码高亮插件双击即可选中,不过部分同学反应,通过代码高亮插件复制的代码无法在IDEA里正常显示,这里提供TEXTAREA的方式,方便复制,谢谢
1. 自行完成练习
根据练习目标尽量自己实现代码效果,期间会碰到疑问,难题,和自己不懂的地方,这些都是必要的过程
2. 带着疑问查看答案
完成过程中,碰到无法解决的问题,带着疑问,查看答案,分析答案的解决思路
3. 查看答案讲解视频
依然有不明白的地方,点开视频讲解,带着疑问,听视频讲解有问题的部分
4. 再做一遍
理解后,再从头做一遍,把有疑问的地方都捋清楚
5. 总结
最后再总结一遍,总结思路,总结解决办法,以后遇到类似的问题,怎么处理
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
提问之前请登陆
|