【问题标题】:NuxtJS passing propsNuxtJS 传递道具
【发布时间】:2020-07-30 17:59:36
【问题描述】:

我有一个问题,所以我在 NuxtJS (vuejs) 中有这样的默认布局

<template>
  <div id="app">
    <Loader @animation:complete = 'loader'/>
    <Header/>
    <nuxt :title = 'title'/>
    <Footer/>
    <BgDecor/>
  </div>
</template>

<script>
import { gsap } from "gsap/dist/gsap";

import Loader from "@/layouts/parts/Loader";
import Header from "@/layouts/parts/Header";
import Footer from "@/layouts/parts/Footer";
import BgDecor from "@/layouts/parts/BgDecor";

export default {
  data() {
    return {
      loaderDone: null,
    };
  },
  components: {
    Loader,
    Header,
    Footer,
    BgDecor,
  },
  head() {
    return {
      titleTemplate: "%s - Product Designer UI/UX Designer Strategist",
      meta: [
        {
          hid: "description",
          name: "description",
          content:
            "Artistic Director, Web / Motion Designer, FrontEnd Developer for over 10 years, specializing in visual communication and web design",
        },
        {
          hid: "keywords",
          name: "keywords",
          content:
            "Product Design, UI, UX, Designer, UI Designer, UX Designer, FrontEnd Developer",
        },
      ],
    };
  },
  methods: {
    loader(e) {
      console.log("yesssssss");
      this.loaderDone = "yup";
    },
  },
  computed: {
    title() {
      return this.loaderDone;
    },
  },
};
</script>

<style>
</style>

我的问题是我在 Nuxt 中有 yup 但不在组件 nuxt 内的页面内 是的,我知道这很复杂,但我想在我的加载器完成后得到一个 $emit 到我的索引页面或其他页面以知道我的加载器已经完成 如果我每次回到页面索引时都将加载器直接放入索引中,我会再次获得加载器,我希望在启动网站时拥有它 所以在开发工具中,我在索引的 nuxt 父级中有是的,但是如果我再次使用道具,似乎需要做很多工作来获取我的加载器完成的信息,我可以开始我的其他动画

如果有人有想法谢谢:) 祝你有个愉快的夜晚

【问题讨论】:

    标签: javascript vue.js nuxt.js


    【解决方案1】:

    您可以通过 vuex 商店来做到这一点。

    您的商店可能如下所示

    const store = new Vuex.Store({
      state: {
        loaderDone: false
      },
      mutations: {
        setLoadingDone (state) {
          state.loadingDone = true
        }
      }
    })
    

    在你的加载器中你可以map the mutation,在你的组件中你可以map the state

    要设置完成标志,您可以在加载程序中执行此操作以使用方法setLoadingDone()

    // loader.vue
    import { mapMutations } from 'vuex}
    export default {
      // ...
      methods: {
        ...mapMutations(['setLoadingDone '])
      }
    }
    

    你的其他组件可以像这样得到这个值

    // other components
    import { mapState } from 'vuex}
    export default {
      // ...
      computed: {
        ...mapState(['loadingDone '])
      }
    }
    

    【讨论】:

    • 你好,thopaw,首先谢谢,但它没有用我将控制台日志放入 setLoadingDone 函数的突变中,但我的加载器中没有任何内容,setLoadingDone 到 loader.vue 是一个函数同名太对了?我试图制作endLoader() { console.log("fini"); this.loadingScreen = false; }, setLoadingDone() { this.endLoader; console.log("hello"); this.tlHome.play(); }, ...mapMutations(["setLoadingDone"]),,但我没有得到任何东西,甚至没有来自vuex突变的控制台日志:D
    • 嗨,我再次将 setLoadingDone 放入加载程序的 onComplete 中,它似乎触发了但我有那个“未知突变类型:setLoadingDone”,即使在我的商店中我有 `import ProjectService from '@/services/ ProjectService.js' export const state = () => ({ loaderDone: false }) export const mutation = { setLoadingDone(state) { console.log('mutation loader') state.loadingDone = true } } 所以我不知道为什么
    • 您必须检查文档中的突变 (vuex.vuejs.org/guide/mutations.html)。似乎设置中有错误。 vue devtools 也是调试突变的好帮手。
    • 谢谢,这意味着我的代码 id 不正确或者 vuejs 有问题?无论如何,我查看了突变并提交以获取值,但仍然是错误Uncaught TypeError: Cannot read property 'commit' of undefined 我不知道为什么,因为我有状态(我的 const 称为状态而不是存储),无论如何我用 vuex 的 mapMutations 调用它谢谢为您的帮助:)
    猜你喜欢
    • 2021-06-18
    • 1970-01-01
    • 2017-10-03
    • 1970-01-01
    • 2019-04-10
    • 2018-11-20
    • 2018-05-29
    • 2018-08-01
    • 2018-05-22
    相关资源
    最近更新 更多