1

I am new in Vue.js. i don't know how can i render four message like this

hi
bye

go
back

my result is render go, back. i think my code is not render hello1.vue component. i want render hello, hello1 components. how can i fix it?

hello.vue

<template>
  <P> {{ hello.a }} </p>
  <p> {{ hello.b }} </p>
</template>

<script>
export default {
    data(){
       return{
          hello:{
            a = "hi",
            b = "bye"
       }
    },
    props: ['hello1']
}
</script>

hello2.vue

<template>
  <hello-vue :hello="hello1" />
</template>

<script>
import helloVue from './hello.vue'
export default {
    data(){
       return{
          hello1:{
            a = "go",
            b = "back"
       }
    },
    components:{
       'hello-vue': helloVue
    }
}
</script>

1 Answer 1

1

You are passing the data in a bit of a messy way. So first thing is we need to split this into a parent and child component. The child component will print out your two lines, while parent component will call and pass data.

Secondly in Hello.vue you have props AND data, the template is only accessing hello and not hello1 meaning the props variable isnt parsed.

Thirdly <template> may have only 1 child so that will cause rendering issues as well.

There are different ways, but let's try this

HelloItem.vue

<template>
  <div>
    <P> {{ hello.a }} </p>
    <p> {{ hello.b }} </p>
  </div>
</template>

<script>
export default {
    data() {
       return { }
    },
    props: ['hello']
}
</script>

And now we call this twice by passing in data HelloView.vue

<template>
  <div>
    <hello-item :hello="hello1"/>
    <hello-item :hello="hello2"/>
  </div>
</template>

<script>
import HelloItem from './HelloItem.vue'
export default {
    data() {
       return {
          hello1:{
            a: "hi",
            b: "bye"
          },
          hello2:{
            a: "go",
            b: "back"
          },
       }
    },
    components:{
       'hello-item': HelloItem
    }
}
</script>

Let me know if this answers your question.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.