vue父子组件间传值

你的名字 2022-10-02 11:50 373阅读 0赞

子组件

  1. <template>
  2. <div class="confirm_button">
  3. <el-button type="primary" @click="getButtonClick">{ { text || 确定}}</el-button>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: "confirm-button",
  9. props:["text"],
  10. watch: { },
  11. data: function(){
  12. return{
  13. msg: this.text=="提交"?"提交成功":true
  14. }
  15. },
  16. //方法集
  17. methods:{
  18. getButtonClick(){
  19. this.$emit("message",this.msg)
  20. }
  21. },
  22. }
  23. </script>
  24. <!-- Add "scoped" attribute to limit CSS to this component only -->
  25. <style scoped>
  26. </style>

父组件

  1. <template>
  2. <div class="hello">
  3. { { msg}}
  4. <confirm text="提交" @message="getMessage"></confirm>
  5. <ul id="treeDemo" class="ztree"></ul>
  6. <!-- <div id="jq" @click="initZtreeMethod">hello world</div> -->
  7. </div>
  8. </template>
  9. <script>
  10. import $ from '../assets/jquery-vendor.js'
  11. import 'ztree/js/jquery.ztree.core'
  12. import 'ztree/js/jquery.ztree.excheck'
  13. import 'ztree/css/zTreeStyle/zTreeStyle.css'
  14. import confirm from './sub/Confirm'
  15. export default {
  16. name: "hello",
  17. components:{
  18. confirm
  19. },
  20. data: function(){
  21. return{
  22. zNodes:[
  23. { id:0,pId:null,name:"root"},
  24. { id:1,pId:0,name:"child"}
  25. ],
  26. setting:{
  27. data:{
  28. keep:{
  29. leaf: false,
  30. parent: false
  31. },
  32. simpleData:{
  33. enable: true,
  34. idKey: 'id',
  35. pIdKey: 'pId',
  36. rootPId: null
  37. },
  38. }
  39. },
  40. msg: "Welcome to my first app"
  41. }
  42. },
  43. //方法集
  44. methods:{
  45. initZtreeMethod(){
  46. $.fn.zTree.init($("#treeDemo"), this.setting, this.zNodes);
  47. },
  48. getMessage(val){
  49. this.$message(val+"");
  50. }
  51. },
  52. mounted(){
  53. this.initZtreeMethod();
  54. },
  55. created(){
  56. },
  57. }
  58. </script>
  59. <!-- Add "scoped" attribute to limit CSS to this component only -->
  60. <style scoped>
  61. </style>

效果展示

image

讲解

  • 父组件向子组件传值使用props

    //一些props使用的语法

    //静态
    props: [“for-child-msg”]

    //动态props
    props: {
    // 基础类型检测, null意味着任何类型都行
    propA: Number,
    // 多种类型
    propB: [String, Number],
    // 必传且是String
    propC: {
    type: String,
    required: true
    },
    // 数字有默认值
    propD: {
    type: Number,
    default: 101
    },
    // 数组、默认值是一个工厂函数返回对象
    propE: {
    type: Object,
    default: function() {

    1. console.log("propE default invoked.");
    2. return { message: "I am from propE." };

    }
    },
    // 自定义验证函数
    propF: {
    isValid: function(value) {

    1. return value > 100;

    }
    }
    }

上面是props语法,子组件需要使用props来接受父组件的传值,父组件上需要定义一个和props里面相同的属性,然后给属性赋值。

  • 子组件向父组件传值需要使用this.$emit(“方法名”,“传递的值”),点击的时候会触发getButtonClick方法,然后触发message方法,在父组件也就是getMessage方法,其中的方法参数就是子组件向父组件传递的值.

发表评论

表情:
评论列表 (有 0 条评论,373人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue父子组件

    Vue父子组件传值:有四种方式:props,ref,emit 和模板传递通信slot 通过props来传值: 静态传值就是直接通过props来传递 动态传值是通