加入收藏 | 设为首页 | 会员中心 | 我要投稿 西安站长网 (https://www.029zz.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

vue 组件基础知识总结

发布时间:2021-02-04 13:04:48 所属栏目:编程 来源:脚本之家
导读:副标题#e# 这篇文章主要介绍了vue 组件基础知识的相关资料,帮助大家更好的理解和使用vue的组件,感兴趣的朋友可以了解下 组件基础 1 组件的复用 组件是可复用的Vue实例。 !DOCTYPE html html head meta charset="utf-8" style /style script src="https://

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8"> 
  <style>
  
  </style>
  <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
 </head>
 <body>
  <div id="app">
   <div :style="{fontSize: postFontSize + 'em'}">
    <blog-post v-for="post in posts"
     v-bind:key="post.id"
     v-bind:post="post"
     v-on:enlarge-text="postFontSize += $event" />
   </div>   
  </div>
  <script>
   Vue.component('blog-post', {
    props: ['post'],
    template: `
     <div class="blog-post">
      <h3>{{ post.title }}</h3>
      <button v-on:click="$emit('enlarge-text', 0.2)">放大字体</button>
      <div v-html="post.content"></div>
     </div>
    `
   })

   new Vue({
    el: '#app',
    data: {
     postFontSize: 1,
     posts: [
      { id: 1, title: 'My journey with Vue', content: 'my journey...' },
      { id: 2, title: 'Blogging with Vue', content: 'my blog...' },
      { id: 3, title: 'Why Vue is so fun', content: 'Vue is so fun...' }
     ]
    }
   });
  </script>
 </body>
</html>

在父组件中,我们可以通过$event访问到被抛出的这个值。

我们可以在组件上使用v-model。

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8"> 
  <style>
  
  </style>
  <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
 </head>
 <body>
  <div id="app">
   <!-- <input v-model="searchText"> -->
   <input v-bind:value="searchText" v-on:input="searchText = $event.target.value">
   <p>{{ searchText }}</p>
  </div>
  <script>
   new Vue({
    el: '#app',
    data: {
     searchText: ''
    }
   });
  </script>
 </body>
</html>
<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8"> 
  <style>
  
  </style>
  <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
 </head>
 <body>
  <div id="app">
   <custom-input v-model="searchText"></custom-input>
   <custom-input v-bind:value="searchText" v-on:input="searchText = $event"></custom-input>
   <p>{{ searchText }}</p>
  </div>
  <script>
   Vue.component('custom-input', {
    props: ['value'],
    template: `<input v-bind:value="value" v-on:input="$emit('input', $event.target.value)" >`
   })

   new Vue({
    el: '#app',
    data: {
     searchText: ''
    }
   });
  </script>
 </body>
</html>

最后,注意解析 DOM 模板时的注意事项。

以上就是vue 组件基础知识总结的详细内容,更多关于vue 组件的资料请关注脚本之家其它相关文章!

来源:脚本之家

链接:https://www.jb51.net/article/204818.htm

(编辑:西安站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读