Blogger Information
Blog 40
fans 0
comment 1
visits 34252
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
VUE之组件及传参方式
景云
Original
1139 people have browsed it

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. });

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

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>`之间无内容时,将会把默认值显示出来。
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:得抓紧时间了
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!