Vue JS 道具未定义

Vue JS props is underfined

我正在为 VUE JS 使用 Gridsome 框架

我正在使用 this.$router.push(PATH, PARAMS)

导航到新页面
this.$router.push({path: `/${value.sectionLink}/`, params: {pageOBJ: value}})

页面路由工作正常 - 但是 PROP pageOBJ 在页面中未定义,如在 VUE 检查器中所见:

它应该是一个 object(这是 VALUE 设置的值)即

backgroundColor: (...)
sectionLink: (...)
sectionSubTitle: (...)
sectionTitle: (...)
titleColor: (...)

我已经尝试了多种不同的技术来解决这个问题,但都没有弄明白,所以我想我在这里遗漏了什么?

是这个问题吗?

Gridsome 也引用了 graphQI,你应该使用图形而不是通过推送 Props 来获取数据吗?

任何帮助都会很棒 - 如果您需要任何进一步的信息,请告诉我。

谢谢 - W


根据当前答案更新:

我已经将 props:true 添加到组件中,如下所示,但问题仍然存在。


代码片段 - 摘要:

用户点击 SectionTitle 组件,然后发出页面 link (每个 SectionTitle 都是来自 : sections array of Object 的一个对象)

要查看当前在线版本,请查看

wtwd.ninjashotgunbear.com


<template>
  <Layout>
    <div class="navs" v-for="section in sections" :key="section.sectionTitle">
      <!-- On click delay for screen to come ove top -->
      <!-- router to be put here -->
      <SectionTitle :data="section" @routeChange="reRoute($event)"/>
    </div>
    
    <PageTransition :dataObj="transitionObj"/>
    
  </Layout>
</template>

<script>

import SectionTitle from '@/components/SectionTitle.vue';
import PageTransition from '@/components/PageTransition.vue'

export default {
  metaInfo: {
    title: 'Hello, world!'
  },
  components: {
    SectionTitle,
    PageTransition
  },
  data(){
    return {
      // data to be passed to the components

      sections: [
        {
          sectionTitle: 'Clients',
          sectionLink: 'clients',
          sectionSubTitle: '"Some of the amazing humans I have had the pleasure of working with"',
          backgroundColor: '#F08080',
          titleColor: '#E65454'
        }, 
        {
          sectionTitle: 'Projects',
          sectionLink: 'projects',
          sectionSubTitle: '"I LIKE TO MAKE PROJECTS THAT WILL TEST MY KNOWEDGE AND EXPOSE MY WEAKNESSES"',
          backgroundColor: '#20B2AA',
          titleColor: '#11DACF'
        }, 
        {
          sectionTitle: 'Skills',
          sectionLink: 'skills',
          sectionSubTitle: `"LEARNING WILL NEVER END, SO LONG AS YOUR AMBITIONS ARE STOKED, AND I've got plenty of ambition"`,
          backgroundColor: '#A921B2',
          titleColor: '#CA14D6'
        },
        {
          sectionTitle: 'About Me',
          sectionLink: 'aboutme',
          sectionSubTitle: `"My joruney becoming a developer so far"`,
          backgroundColor: '#FFFFFF',
          titleColor: '#D1D1D1'
        },
        {
          sectionTitle: 'Contact Me',
          sectionLink: 'contactme',
          sectionSubTitle: `"If you have any questions or want to reach out about a project then i'd love to speak with you"`,
          backgroundColor: '#2185B2',
          titleColor: '#0076AB'
        }
      ],
  
      divText: null,

      transitionObj: null
  
  }

  },
  methods:{
    reRoute(value){
      
      // 1)A) - change the text of the div to say the section it is being moved to
      this.divText = value.sectionTitle
      this.transitionObj = value
    
     // FIND center pixcle value to place text - scrolled height + windowHeight / 2 = centerPos
      let centerPos = window.scrollY+(window.innerHeight/2)

      // Apply secific Title color - and center possitioning
      document.querySelector('.leaveScreen p').style.cssText=`color: ${value.titleColor}; top: ${centerPos}px`


      // 1) animate the loading screen
        let screen = document.querySelector('.leaveScreen');
        screen.style.cssText=`background: ${value.backgroundColor}; left: 0%`;

      // 2) re-route the page
      setTimeout(()=>{
        this.$router.push({path: `/${value.sectionLink}/`, params: {pageOBJ: value}}) // << says that the route name is not found
        // this.$router.push(value.sectionLink)
      }, 700)

    }
  }
}
  
</script>

<style lang="scss"> 
//  **** ERROR CAUSED BY NOT INSTALLING SCSS package ****
// https://gridsome.org/docs/assets-css/ &&&& npm install -D sass-loader node-sass

// Universal Font being used - LEMON MILK
@font-face {
  font-family: LemonMilk;
  src: url('../assets/fonts/LemonMilk.otf');
}

* {
  box-sizing: border-box;
}

.navs {
    font-family: LemonMilk;
}

.SectionTitle{
  cursor: pointer;
}

</style>

this.$router.push()

中传递 name 而不是 path
this.$router.push({name: ${value.sectionLink}, params: {pageOBJ: value}})

您应该将 props:true 添加到路由定义中:

{
 path:'/thepath/:theParam',
 component:SomeComponent,
 props:true
}