【问题标题】:Vue HOC setup issue (with vue-compose)Vue HOC 设置问题(使用 vue-compose)
【发布时间】:2019-02-08 10:50:09
【问题描述】:

我正在尝试按照函数式编程进行设置。出于某种原因,我的index.js 文件没有将道具传递给我HelloWorld.vue 组件。 下面是 index.js 文件。

import { compose, withData, withProps, withHandlers } from 'vue-compose';
import Vue from 'vue';
import HelloWorld from './HelloWorld.vue';


export const enhancer = compose(
    withData({
      someInjectedProp: {
        initialValue: "Bla bla"
      },
      ecosystem:  {
          iniitalValue: [
            {
              text: 'vuetify-loader',
              href: 'https://github.com/vuetifyjs/vuetify-loader'
            },
            {
              text: 'github',
              href: 'https://github.com/vuetifyjs/vuetify'
            },
            {
              text: 'awesome-vuetify',
              href: 'https://github.com/vuetifyjs/awesome-vuetify'
            }
          ],
      },
      importantLinks: {
          initialValue: [
            {
              text: 'Documentation',
              href: 'https://vuetifyjs.com'
            },
            {
              text: 'Chat',
              href: 'https://community.vuetifyjs.com'
            },
            {
              text: 'Made with Vuetify',
              href: 'https://madewithvuetifyjs.com'
            },
            {
              text: 'Twitter',
              href: 'https://twitter.com/vuetifyjs'
            },
            {
              text: 'Articles',
              href: 'https://medium.com/vuetify'
            }
          ],
      },
      whatsNext: {
          iniitalValue: [
            {
              text: 'Explore components',
              href: 'https://vuetifyjs.com/components/api-explorer'
            },
            {
              text: 'Select a layout',
              href: 'https://vuetifyjs.com/layout/pre-defined'
            },
            {
              text: 'Frequently Asked Questions',
              href: 'https://vuetifyjs.com/getting-started/frequently-asked-questions'
            }

          ] 
      }
    }),
    // withProps((props) => ({
    //   customFunction(this: Vue){
    //     //do something like call api
    //   }
    // }))
  )

  export default enhancer(HelloWorld);

下面是 vue 组件,它应该接收道具但不是:

  <v-container>
    <v-layout
      text-xs-center
      wrap
    >
      <v-flex xs12>
        <v-img
          :src="require('../assets/logo.svg')"
          class="my-3"
          contain
          height="200"
        ></v-img>
      </v-flex>

      <v-flex mb-4>
        <h1 class="display-2 font-weight-bold mb-3">
          Welcome to Vuetify
        </h1>
        <p class="subheading font-weight-regular">
          For help and collaboration with other Vuetify developers,
          <br>please join our online
          <a href="https://community.vuetifyjs.com" target="_blank">Discord Community</a>
        </p>
      </v-flex>

      <v-flex
        mb-5
        xs12
      >
        <h2 class="headline font-weight-bold mb-3">What's next?</h2>

        <v-layout justify-center>
          <a
            v-for="(next, i) in whatsNext"
            :key="i"
            :href="next.href"
            class="subheading mx-3"
            target="_blank"
          >
            {{ next.text }}
          </a>
        </v-layout>
      </v-flex>

      <v-flex
        xs12
        mb-5
      >
        <h2 class="headline font-weight-bold mb-3">Important Links</h2>

        <v-layout justify-center>
          <a
            v-for="(link, i) in importantLinks"
            :key="i"
            :href="link.href"
            class="subheading mx-3"
            target="_blank"
          >
            {{ link.text }}
          </a>
        </v-layout>
      </v-flex>

      <v-flex
        xs12
        mb-5
      >
        <h2 class="headline font-weight-bold mb-3">Ecosystem</h2>

        <v-layout justify-center>
          <a
            v-for="(eco, i) in ecosystem"
            :key="i"
            :href="eco.href"
            class="subheading mx-3"
            target="_blank"
          >
            {{ eco.text }}
          </a>
        </v-layout>
      </v-flex>
    </v-layout>
  </v-container>
</template>

<script>
  export default {
    props: ['someInjectedProp','ecosystem','importantLinks','whatsNext']
  }
</script>

<style>

</style>

查看 vue devtools 时,我可以看到所有的 props 都是 undefined 并且没有收到索引文件中定义的值。

【问题讨论】:

    标签: javascript vue.js functional-programming high-order-component


    【解决方案1】:

    我的一位同事发现了问题,它实际上是在app.vue 文件中(在运行create vue &lt;app name&gt; 时创建的),当使用HOC 时,导入会查找.\components\HelloWorld.vue,它需要导入.\components\index

    【讨论】:

      猜你喜欢
      • 2020-09-18
      • 2019-05-27
      • 2018-10-04
      • 2019-09-02
      • 2021-07-16
      • 2020-04-08
      • 2018-02-13
      • 2020-07-12
      • 2019-03-06
      相关资源
      最近更新 更多