【问题标题】:Importing a component into Vue and then passing data将组件导入Vue,然后传递数据
【发布时间】:2018-12-19 20:47:26
【问题描述】:

我的 Nuxt 项目中有一个组件。

组件/Boxes.vue:

<template>
  <b-container>
    <b-row>
        <b-col v-for="box in boxes" v-bind:key="box">
          <b-card 
          :title="box.title"
          class="text-center mt-5">
            <p class="card-text">
              {{ box.text }}
            </p>
            <b-button
            variant="danger"
            >Find out more</b-button>
          </b-card>
        </b-col>
    </b-row>
  </b-container>
</template>

<script>
export default {
  name: "Boxes",
  data() {
    return {
    }
  }
}
</script>

那我要导入到pages/index.vue中

<template>
<div id="home">
  <b-container>
    <b-carousel 
      id="carousel1"
      class="mt-2"
      style="text-shadow: 1px 1px 2px #333;"
      controls
      indicators
      background="#ababab"
      :interval="4000"
      img-width="1024"
      img-height="480"
      v-model="slide"
      @sliding-start="onSlideStart"
      @sliding-end="onSlideEnd"
    >
      <b-carousel-slide img-src="./slides/slide1.jpg"></b-carousel-slide>
      <b-carousel-slide img-src="./slides/slide2.jpg"></b-carousel-slide>
      <b-carousel-slide img-src="./slides/slide3.jpg"></b-carousel-slide>
      <b-carousel-slide img-src="./slides/slide4.jpg"></b-carousel-slide>
    </b-carousel>
  </b-container>
  <Boxes/>
</div>
</template>

<script>
import Boxes from '@/components/Boxes.vue';

export default {
  components: {
    Boxes
  },
  data () {
    return {
      slide: 0,
      sliding: null,
      boxes: [
        {
          title: 'Fire Stopping',
          text: 'How does it work?'
        },
        {
          title: 'Our Services',
          text: 'Full range of firestopping'
        },
        {
          title: 'Bid Request',
          text: 'Inexpensive peace of mind'
        },
      ]
    }
  },
  methods: {
    onSlideStart (slide) {
      this.sliding = true
    },
    onSlideEnd (slide) {
      this.sliding = false
    }
  }
}
</script>

当我尝试将它加载到 index.vue 文件时,它就像我没有导入它一样。

你看到它在这里运行,所以你可以更好地理解:https://codesandbox.io/s/github/perfectimprints/precisionfirestopping.com

Vue 抱怨组件没有定义,但是在渲染时注册了。

【问题讨论】:

  • 那个组件有一些错误
  • @BoussadjraBrahim 需要详细说明吗?

标签: javascript vue.js vuejs2 vue-component nuxt.js


【解决方案1】:

您缺少将 boxes 道具传递给您的组件:

    <Boxes :boxes="boxes" />

在组件内部你应该添加props:['boxes'] 像:

   <script>
      export default {
        name: "Boxes",
        props:['boxes'],
        data() {}
       }
    </script>

检查这个code

您可以在这些doc1doc2 中了解props 以及如何将数据从父组件传递到子组件

【讨论】:

  • 成功了,谢谢!您能否将我链接到一些文档或解释为什么会这样?
  • 非常感谢,我会好好学习的。此外,当我尝试传递类似以下内容时:&lt;b-button :href="link"&gt;Button&lt;/b-button&gt; it gives me the error: Cannot use 'in' operator to search 'link' in undefined`
  • 您在哪个组件中拥有它?
  • 当我尝试添加到Boxes 组件时。
  • 我修好了。在页面上插入我的组件现在看起来像这样:&lt;Boxes v-for="box in boxes" v-bind:key="box.title" :title="box.title" :text="box.text" /&gt; 应该是这样。感谢您的所有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-07
  • 2016-09-24
  • 2020-06-13
  • 1970-01-01
  • 2018-02-07
  • 2017-02-27
相关资源
最近更新 更多