1.组件

  • 可以理解为一个自定义html标签
  • 是可复用的vue实例,要构造函数Vue的子类

    使用步骤:

    1 创建

  1. const childComponent = Vue.extend({
  2. template: `<h2>Hello Wrold</h2>`,
  3. });

2.注册

Vue.component("child-component", childComponent);

  • 使用Vue.component()静态方法注册的是称为:全局组件
  • 组件名: 自定义的html标签

    3.挂载

  1. const vm = new Vue({
  2. el: "#root",
  3. });

可将第一、二步省略,简写为:Vue.component(组件名,对象字面量表示的组件配置项);再进行第三步挂载,可以达到同样的效果。

  1. Vue.component("child-component", {
  2. template: `<h2>Hello Wrold</h2>`,
  3. });

HTML代码

  1. <div id="root">
  2. <child-component></child-component>
  3. <child-component></child-component>
  4. </div>

———————————————————————

2.全局组件

  • 全局可见,声明在vue实例外部
  • 组件中的模板代码: 允许存在数据占位符的html字符串
  • 模板内容必须写到一对父元素中
  • 组件中必须使用函数data()来声明组件变量
  • 全局组件可以在多个vue实例共享
  • 建议: 通常一个项目只有一个vue实例,所以尽量不要用全局组件,应该使用局部组件

    第1种用法:

    HTML代码
  1. <div id="root">
  2. <button-inc></button-inc>
  3. </div>
  1. Vue.component("button-inc", {
  2. template: `
  3. <div>
  4. <button @click="count++">点赞: + {{count}}</button>
  5. </div>
  6. `,
  7. data() {
  8. return {
  9. count: 0,
  10. };
  11. },
  12. });
  13. const vm = new Vue({
  14. el: "#root",
  15. });

第2种用法:

  1. Vue.component("button-inc", {
  2. template: "#inc",
  3. data() {
  4. return {
  5. count: 0,
  6. };
  7. },
  8. });

HTML代码

  1. <div id="root">
  2. <button-inc></button-inc>
  3. </div>
  4. <template id="inc">
  5. <div>
  6. <button @click="count++">点赞: + {{count}}</button>
  7. </div>
  8. </template>

——————————————————————-

3.局部组件(components)

  • 局部组件是属于vue实例的
    HTML代码
  1. <div id="root">
  2. <hellos></hellos>
  3. </div>
  4. <template id="hello">
  5. <p>Hello2 {{site}}</p>
  6. </template>
  1. const hellos = {
  2. template: '#hello',
  3. data() {
  4. return {
  5. site: "php中文网",
  6. };
  7. },
  8. };
  9. const vm = new Vue({
  10. // el: "#root",//可将此步用后面.$mount("#root")代替
  11. components: { hellos },
  12. }).$mount("#root");

————————————————————-

4. 组件之间的传参: 父组件向子组件传参

  • 父组件通过自定义属性的方式将参数传到子组件中
  1. <div id="app">
  2. <btn-inc :my-name="username" :my-count="count"><btn-inc>
  3. </div>
  1. const vm = new Vue({
  2. el: document.querySelector("#app"),
  3. data() {
  4. return {
  5. username: "有声的紫荆",
  6. count: 0,
  7. };
  8. },
  9. // 局部组件
  10. //组件之间的数据传递是单向的,不允许在子组件中更新父组件中的数据
  11. components: {
  12. btnInc: {
  13. props: ["myName", "myCount"],
  14. template: `
  15. <div>
  16. <button @click="num++">点赞: + {{num}}</button>
  17. <span>{{myName}}</span>
  18. </div>
  19. `,
  20. data() {
  21. return {
  22. num: this.myCount,
  23. };
  24. },
  25. },
  26. },
  27. });

——————————————————

5.组件之间的传参: 子组件向父组件传参

  • 通过声明同名事件来实现
  • $emit(父组件中要使用的方法名称,子组件要传给父组件的值 )
  1. <div id="app">
  2. <btn-inc :my-name="username" :my-count="count" @click-count="handle"></btn-inc>
  3. </div>
  1. const vm = new Vue({
  2. el: document.querySelector("#app"),
  3. data() {
  4. return {
  5. username: "有声的紫荆",
  6. count: 0,
  7. };
  8. },
  9. // 局部组件
  10. components: {
  11. btnInc: {
  12. props: ["myName", "myCount"],
  13. template: `
  14. <div>
  15. <button @click="$emit('click-count',10 )">点赞: + {{myCount}}</button>
  16. <span>{{myName}}</span>
  17. </div>
  18. `,
  19. },
  20. },
  21. // 父组件更新数据的方法
  22. methods: {
  23. handle(value) {
  24. this.count += value;
  25. this.username = "天蓬老师";
  26. },
  27. },
  28. });

——————————————————-

6.组件之间双向传参

  1. <div id="app">
  2. <p>父组件: {{price}} 元</p>
  3. <p>
  4. <span>子组件:</span>
  5. <my-input :my-price="price" @input-text="handle"></my-input>
  6. </p>
  7. </div>
  1. const vm = new Vue({
  2. el: document.querySelector("#app"),
  3. data() {
  4. return {
  5. price: 4588,
  6. };
  7. },
  8. // 局部组件
  9. components: {
  10. "my-input": {
  11. props: ["my-price"],
  12. template: `
  13. <input type="text" :value="myPrice" @input="foo" />
  14. `,
  15. methods: {
  16. foo(ev) {
  17. this.$emit("input-text", ev.target.value);
  18. },
  19. },
  20. },
  21. },
  22. methods: {
  23. handle(value) {
  24. this.price = value;
  25. },
  26. },
  27. });

———————————————————

)" class="reference-link">7.插槽: 组件内容分发(<slot></slot>)

  1. <div id="app">
  2. <my-comp>Wrold</my-comp>
  3. </div>
  1. new Vue({
  2. el: "#app",
  3. components: {
  4. myComp: {
  5. template: `
  6. <div>
  7. <h2>Hello <slot>哈哈哈</slot> </h2>
  8. </div>
  9. `,
  10. },
  11. },
  12. });
  13. //其中“哈哈哈”为默认值,当`<my-comp></my-comp>`之间无内容时,将会把默认值显示出来。

更多相关文章

  1. DAY03-实例演示选择器优先级、前端组件样式模块化实现、伪类选择
  2. 4.2 vSphere软件组件详解
  3. Vue组件常用的六种通信方式
  4. 通过keep-alive实现了解vue组件实现原理(3)
  5. 源码分析vuex如何维护多个组件的数据共享
  6. vue与react的简单比较
  7. vue与react的简单比较(全局状态管理)
  8. 在h5中如何利用antd和element-ui的底层组件逻辑自定义组件样式?
  9. VMware vSphere虚拟化-VMware ESXi 5.5组件安装过程记录

随机推荐

  1. php实现进度条原理
  2. php自动生成不重复的id
  3. php中ajax的使用实例讲解
  4. 支付宝app登录授权的infoStr授权登录流程
  5. php数组打乱顺序
  6. 关于php中对象传值方式的详解
  7. php源码加密方法详解
  8. php数组转json
  9. php中substr_compare()区分大小写吗
  10. php实现分页的原理及步骤