{"id":984,"date":"2023-03-23T22:41:43","date_gmt":"2023-03-23T14:41:43","guid":{"rendered":""},"modified":"2023-03-23T22:41:43","modified_gmt":"2023-03-23T14:41:43","slug":"Vue.js \u81ea\u5b9a\u4e49\u6307\u4ee4","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/984.html","title":{"rendered":"Vue.js \u81ea\u5b9a\u4e49\u6307\u4ee4"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Vue.js \u81ea\u5b9a\u4e49\u6307\u4ee4<\/h1>\n<\/p><\/div>\n
\n Vue.js\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u4e86Vue.js\u6307\u4ee4\uff0c\u4ee5\u4f7f\u5176\u53ef\u91cd\u7528\u548c\u76f4\u63a5\u3002\u6307\u4ee4\u5c31\u50cf\u53ef\u4ee5\u5728Vue.js\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7528\u4e8e\u4ee5\u7279\u5b9a\u65b9\u5f0f\u6267\u884c\u64cd\u4f5c\u7684\u6307\u4ee4\u4e00\u6837\u3002\u6211\u4eec\u5df2\u7ecf\u5728https:\/\/www.javatpoint.com\u4e2d\u4f7f\u7528\u4e86\u7b80\u5355\u7684\u6761\u4ef6\u6307\u4ee4\uff0c\u4f8b\u5982v-if\uff0cv-show\uff0cv-else\uff0cv-for\uff0cv-bind\uff0cv-model\uff0cv-on\u7b49\u3002\/ vue-js-conditions-and-loops\u9875\u9762\u3002\n <\/div>\n
\n \u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u521b\u5efa\u81ea\u5b9a\u4e49\u6307\u4ee4\u548c\u5168\u5c40\u6307\u4ee4\uff0c\u5e76\u50cf\u5728Vue\u4e2d\u4e00\u6837\u4f7f\u7528\u5b83\u4eec\u3002js\u7ec4\u4ef6\uff0c\u56e0\u4e3aVue.js\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u81ea\u5df1\u7684\u81ea\u5b9a\u4e49\u6307\u4ee4\u3002\n <\/div>\n
\n \u8bed\u6cd5: <\/strong>\n <\/div>\n
\n
 Vue.directive('name_of_the_directive', {
   bind(e1, binding, vnode) {
   }
})
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8981\u521b\u5efa\u81ea\u5b9a\u4e49\u6307\u4ee4\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528Vue.directive\u548c\u6307\u4ee4\u540d\u79f0\uff0c\u5982\u4e0a\u9762\u7684\u8bed\u6cd5\u6240\u793a\u3002\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u793a\u4f8b\u6765\u6f14\u793a\u5982\u4f55\u521b\u5efa\u81ea\u5b9a\u4e49\u6307\u4ee4\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u3002\n <\/div>\n
\n Index.html\u6587\u4ef6: <\/strong>\n <\/div>\n
\n
 <html><\/span>
   <head><\/span>
      <title><\/span>Vue.js Custom Directive<\/title><\/span>
      <link rel=\"stylesheet\" href=\"index.css\">
        <script src=\"https:\/\/cdn.jsdelivr.net\/npm\/vue\/dist\/vue.js\"><\/script><\/span>
    <\/head><\/span>
    <body><\/span>
      <div id = \"directive\">
          <div v-changestyle>Vue.js Custom Directive Example<\/div><\/span>
      <\/div><\/span>
       <script type = \"text\/javascript\">
         Vue.directive(\"changestyle\",{
            bind(e1,binding, vnode) {
               console.log(e1);
               e1.style.color = \"red\";
               e1.style.fontSize = \"20px\";
            }
         });
         var vm = new Vue({
            el: '#directive',
            data: {
            },
            methods : {
            },
         });
      <\/script><\/span>
      <script src=\"index.js\"><\/script><\/span>
   <\/body><\/span>
<\/html><\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n Index.js\u6587\u4ef6: <\/strong>\n <\/div>\n
\n
 var vm = new Vue({
            el: '#directive',
            data: {
            },
            methods : {
            },
         })
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8ba9\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684CSS\u6587\u4ef6\u4f7f\u8f93\u51fa\u66f4\u5177\u5438\u5f15\u529b\u3002\n <\/div>\n
\n Index.css\u6587\u4ef6: <\/strong>\n <\/div>\n
\n
 html, body {
    margin: 5px;
    padding: 0;
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7a0b\u5e8f\u6267\u884c\u540e\uff0c\u60a8\u5c06\u770b\u5230\u4ee5\u4e0b\u8f93\u51fa:\n <\/div>\n
\n \u8f93\u51fa: <\/strong>\n <\/div>\n

Vue.js \u81ea\u5b9a\u4e49\u6307\u4ee4_https:\/\/bianchenghao6.com_\u3010vue\u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\u793a\u4f8b\u8bf4\u660e<\/h3>\n
\n \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a\" changestyle\"\u7684\u81ea\u5b9a\u4e49\u6307\u4ee4\uff0c\u5982\u4e0b\u6240\u793a:\n <\/div>\n
\n
 Vue.directive(\"changestyle\",{
   bind(e1,binding, vnode) {
      console.log(e1);
      e1.style.color = \"red\";
      e1.style.fontSize = \"20px\";
   }
});
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u81ea\u5b9a\u4e49\u6307\u4ee4\" changestyle\"\u5df2\u5206\u914d\u7ed9div\uff0c\u5982\u4e0b\u6240\u793a:\n <\/div>\n
\n
 <div v-changestyle>Vue.js Custom Directive Example<\/div><\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5982\u4f55\u5c06\u503c\u4f20\u9012\u7ed9\u81ea\u5b9a\u4e49\u6307\u4ee4\uff1f<\/h3>\n
\n \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u503c\u7ed1\u5b9a\u5230\u81ea\u5b9a\u4e49\u6307\u4ee4\u6765\u5c06\u503c\u4f20\u9012\u7ed9\u81ea\u5b9a\u4e49\u6307\u4ee4\u3002\u7ed1\u5b9a\u5c31\u50cf\u4f20\u9012\u7ed9\u81ea\u5b9a\u4e49\u6307\u4ee4\u7684\u53c2\u6570\u4e00\u6837\u3002\n <\/div>\n
\n \u8bed\u6cd5: <\/strong>\n <\/div>\n
\n
 v-changestyle = \"{color:'the_color_name'}\". 
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8ba9\u6211\u4eec\u4ee5\u4e00\u4e2a\u793a\u4f8b\u6765\u6f14\u793a\u5982\u4f55\u5c06\u503c\u4f20\u9012\u7ed9\u81ea\u5b9a\u4e49\u6307\u4ee4\u3002\n <\/div>\n
\n Index.html: <\/strong>\n <\/div>\n
\n
 <html><\/span>
   <head><\/span>
      <title><\/span>Vue.js Custom Directive<\/title><\/span>
      <link rel=\"stylesheet\" href=\"index.css\">
        <script src=\"https:\/\/cdn.jsdelivr.net\/npm\/vue\/dist\/vue.js\"><\/script><\/span>
    <\/head><\/span>
    <body><\/span>
      <div id = \"directive\">
          <div v-changestyle = \"{color:'green'}\">Passing value to custom directive example<\/div><\/span>
      <\/div><\/span>
      <script type = \"text\/javascript\">
         Vue.directive(\"changestyle\",{
            bind(e1,binding, vnode) {
               console.log(e1);
               console.log(binding.value.color);
               console.log(vnode);
               e1.style.color=binding.value.color;
               e1.style.fontSize = \"20px\";
            }
         });
      <\/script><\/span>
      <script src=\"index.js\"><\/script><\/span>
   <\/body><\/span>
<\/html><\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n Index.js: <\/strong>\n <\/div>\n
\n
 var vm = new Vue({
            el: '#directive',
            data: {
            },
            methods : {
            },
         })
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7a0b\u5e8f\u6267\u884c\u540e\uff0c\u60a8\u5c06\u770b\u5230\u4ee5\u4e0b\u8f93\u51fa:\n <\/div>\n
\n \u8f93\u51fa: <\/strong>\n <\/div>\n

Vue.js \u81ea\u5b9a\u4e49\u6307\u4ee4_https:\/\/bianchenghao6.com_\u3010vue\u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n

\n \u60a8\u53ef\u4ee5\u770b\u5230\u8be5\u503c\u5728\u6307\u4ee4\u4e2d\u4f20\u9012\uff0c\u5e76\u4e14\u6587\u672c\u7684\u989c\u8272\u53d8\u4e3a\u7eff\u8272\u3002\u901a\u8fc7\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u4f20\u9012\u503c:\n <\/div>\n
\n
 <div v-changestyle = \"{color:'green'}\">Passing value to custom directive example<\/div><\/span> 
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u8bbf\u95ee:\n <\/div>\n
\n
 Vue.directive(\"changestyle\",{
   bind(e1,binding, vnode) {
      console.log(e1);
      console.log(binding.value.color);
      console.log(vnode);
      e1.style.color=binding.value.color;
      e1.style.fontSize = \"20px\";
   }
});
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5c06\u8fc7\u6ee4\u5668\u4e0e\u81ea\u5b9a\u4e49\u6307\u4ee4\u4e00\u8d77\u4f7f\u7528<\/h3>\n
\n Vue.js\u652f\u6301\u53ef\u7528\u4e8e\u6587\u672c\u683c\u5f0f\u7684\u8fc7\u6ee4\u5668\u3002\u8fc7\u6ee4\u5668\u901a\u5e38\u4e0ev\u7ed1\u5b9a\u548c\u63d2\u503c({{}})\u4e00\u8d77\u4f7f\u7528\u3002\u6211\u4eec\u8981\u6c42\u5728\u8fc7\u6ee4\u5668
\n JavaScript \u8868\u8fbe\u5f0f\u7684\u672b\u5c3e\u4f7f\u7528\u7ba1\u9053\u7b26\u53f7\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u793a\u4f8b\u6f14\u793a\u5728\u81ea\u5b9a\u4e49\u6307\u4ee4\u4e2d\u4f7f\u7528\u8fc7\u6ee4\u5668\u3002\n <\/div>\n
\n Index.html\u6587\u4ef6: <\/strong>\n <\/div>\n
\n
 <html><\/span>
   <head><\/span>
      <title><\/span>Vue.js Custom Directive<\/title><\/span>
      <link rel=\"stylesheet\" href=\"index.css\">
        <script src=\"https:\/\/cdn.jsdelivr.net\/npm\/vue\/dist\/vue.js\"><\/script><\/span>
    <\/head><\/span>
    <body><\/span>
      <div id = \"directive\">
                  <input v-model = \"name\" placeholder = \"Enter Your Name\" \/><br\/>
         <span style = \"font-size:20px;\"><b><\/span>Letter count is : {{name | countletters}}<\/b><\/span><\/span><\/span>
      <\/div><\/span>
      <\/script><\/span>
      <script src=\"index.js\"><\/script><\/span>
   <\/body><\/span>
<\/html><\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n Index.js\u6587\u4ef6: <\/strong>\n <\/div>\n
\n
 var vm = new Vue({
            el: '#directive',
           data: {
               name : \"\"
            },
            filters : {
               countletters : function(value) {
                  return value.length;
               }
            }
         })
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7a0b\u5e8f\u6267\u884c\u540e\uff0c\u60a8\u5c06\u770b\u5230\u4ee5\u4e0b\u8f93\u51fa:\n <\/div>\n
\n \u8f93\u51fa: <\/strong>\n <\/div>\n

Vue.js \u81ea\u5b9a\u4e49\u6307\u4ee4_https:\/\/bianchenghao6.com_\u3010vue\u6559\u7a0b\u3011_\u7b2c3\u5f20 <\/p>\n

\n \u5728\u4e0a\u9762\u7684\u6846\u4e2d\u952e\u5165\u4efb\u4f55\u6587\u672c\u65f6\uff0c\u60a8\u53ef\u4ee5\u5f97\u5230\u5982\u4e0b\u6240\u793a\u7684\u5b57\u6bcd\u6570:\n <\/div>\n

Vue.js \u81ea\u5b9a\u4e49\u6307\u4ee4_https:\/\/bianchenghao6.com_\u3010vue\u6559\u7a0b\u3011_\u7b2c4\u5f20 <\/p>\n

\u793a\u4f8b\u8bf4\u660e<\/h3>\n
\n \u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u60a8\u4f1a\u770b\u5230\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a
\n \" countletters\"\u7684\u7b80\u5355\u8fc7\u6ee4\u5668\u3002<\/strong>
\n countletters <\/strong>\u8fc7\u6ee4\u5668\u4f1a\u8ba1\u7b97\u5728\u6587\u672c\u6846\u4e2d\u8f93\u5165\u7684\u5b57\u7b26\u6570\u3002\u6211\u4eec\u5fc5\u987b\u4f7f\u7528filter\u5c5e\u6027\u5e76\u5b9a\u4e49\u4f7f\u7528\u7684\u8fc7\u6ee4\u5668\uff0c\u5982\u4e0b\u6240\u793a:\n <\/div>\n
\n
 filters : {
   countletters : function(value) {
      return value.length;
   }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5df2\u7ecf\u5b9a\u4e49\u4e86\u65b9\u6cd5
\n countletters <\/strong>\uff0c\u7136\u540e\u8fd4\u56de\u4e86\u8f93\u5165\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002\n <\/div>\n
\n \u73b0\u5728\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u7ba1\u9053\u8fd0\u7b97\u7b26\u548c\u8fc7\u6ee4\u5668\u540d\u79f0
\n \"\u8ba1\u6570\u5668\" <\/strong>\uff0c\u4ee5\u5728\u8f93\u51fa\u4e2d\u663e\u793a\u8fc7\u6ee4\u5668\u7684\u7ed3\u679c:\n <\/div>\n
\n
 <span style = \"font-size:20px;\"><b><\/span>Letter count is : {{name | countletters}}<\/b><\/span><\/span><\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Vue.js \u81ea\u5b9a\u4e49\u6307\u4ee4zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[122],"tags":[],"class_list":["post-984","post","type-post","status-publish","format-standard","hentry","category-vuejc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/984"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=984"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/984\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}