vue 如何将组件作为参数传入传入另一个组件

vue 如何将组件作为参数传入传入另一个组件,vue 如何将组件作为参数传入传入另一个组件

这样做的目的是什么?slot?

说详细一点,你这个感觉是你自己钻死胡同了,举个例子看看你想干什么

是一个子组件要用到另一个子组件里的值或者方法么?

  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7751730
  • 这篇博客你也可以参考下:vue3.0 比 vue2.0 强!在哪里
  • 除此之外, 这篇博客: 如何设计vue组件中的 交互与数据 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 用户的操作往往会带来数据的改变.
    在vue中, 我们要通过 数据驱动视图.

    也就是, 页面中样式, 表格的行数等等, UI的变化.
    要通过改变响应式的数据, 让vue 去帮我们处理UI.

    下面, 是要加入交互的部分了

    我们在 GeneralTable 中加入 data 表格内容.
    但是, 我们发现 SearchBar 的结果会影响到 data

    如果, 我们将 data 放到 GeneralTable 组件中, 我们很难处理 SearchBar 的响应.
    需要再转发一次或者通过vuex.

    更好的解决办法是:
    把 data 放到 FilterableProductTable 中.
    通过处理 SearchBar 的 自定义事件 search 来处理.

    FilterableProductTable 的代码

    <template>
      <div>
        <SearchBar @search="handleSearch"> </SearchBar>
        <GeneralTable :columns="columns" :data="product_data"> </GeneralTable>
      </div>
    </template>
    
    <script>
    import SearchBar from "./SearchBar";
    import GeneralTable from "./GeneralTable";
    
    export default {
      data() {
        return {
          columns: [
            { name: "产品名称", key: "product_name" },
            { name: "价格", key: "price" },
            { name: "是否在售", key: "is_sale" },
          ],
          product_data: [],
        };
      },
      components: {
        SearchBar,
        GeneralTable,
      },
      methods: {
        handleSearch(query) {
          // 这里根据query 去请求数据
          // 仅仅为了展示, 工作中会请求后端的API
          console.log(query);
          this.data = [
            { product_name: "苹果IPhoneX", price: 7111.0, is_sale: "是" },
            { product_name: "华为Note7", price: 5321.0, is_sale: "是" },
            { product_name: "iphone 4", price: 3100.0, is_sale: "否" },
            { product_name: "nova1", price: 2011.0, is_sale: "否" },
          ]
        },
      },
    };
    </script>
    

    SearchBar 的代码

    <template>
      <div>
        <input v-model="query.keyword" type="text" placeholder="根据名称过滤" />
        <label>
          仅展示在售
          <input v-model="query.only_on_sale" type="checkbox" />
        </label>
    
        <button @click="search">搜索</button>
      </div>
    </template>
    
    <script>
    export default {
      created() {
        // 这样可以让父组件初始化时请求数据;
        this.$emit("search", this.query);
      },
      data() {
        return {
          query: {
            keyword: "",
            only_on_sale: false,
          },
        };
      },
      methods: {
        search() {
          this.$emit("search", this.query);
        },
      },
    };
    </script>
    
  • 您还可以看一下 徐照兴老师的Vue全家桶零基础入门到进阶项目实战第一篇入门准备实操篇课程中的 系列课程介绍小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    我可以回答这个问题。

    在Vue中,将一个组件作为参数传递给另一个组件可以通过props实现。

    首先,在父组件中定义一个props,用于接收子组件。

    <template>
      <div>
        <ChildComponent :componentProp="myComponent"/>
      </div>
    </template>
    
    <script>
    import MyComponent from "@/components/MyComponent.vue";
    
    export default {
      data() {
        return {
          myComponent: MyComponent
        };
      }
    };
    </script>
    

    然后在子组件中,接收并渲染传入的组件。

    <template>
      <div>
        <component :is="componentProp" />
      </div>
    </template>
    
    <script>
    
    export default {
      props: {
        componentProp: {
          type: Object,
          required: true
        }
      }
    };
    </script>
    

    上述代码中,父组件通过props向子组件传递一个组件变量myComponent,然后在子组件中通过绑定到componentProp属性接收这个组件变量,并渲染出这个组件。

    如果要传递组件的数据或方法,可以通过在组件中定义props并传递到子组件中。

    <template>
      <div>
        <ChildComponent :componentProp="{
          component: myComponent,
          data: myData,
          method: myMethod
        }"/>
      </div>
    </template>
    
    <script>
    import MyComponent from "@/components/MyComponent.vue";
    
    export default {
      data() {
        return {
          myComponent: MyComponent,
          myData: {
            msg: "Hello World"
          }
        };
      },
      methods: {
        myMethod() {
          console.log("Method from parent component");
        }
      }
    };
    </script>
    

    然后在子组件中,通过props接收数据和方法,并在组件中使用它们。

    <template>
      <div>
        <component :is="componentProp.component" />
        <div>{{ componentProp.data.msg }}</div>
        <button @click="componentProp.method()">Click me</button>
      </div>
    </template>
    
    <script>
    
    export default {
      props: {
        componentProp: {
          type: Object,
          required: true
        }
      }
    };
    </script>
    

    上述代码中,父组件通过props向子组件传递一个包含组件、数据和方法的对象componentProp,然后在子组件中分别使用这些数据和方法。

    这样,就可以在Vue中将一个组件作为参数传递给另一个组件了。