javascript - Vue.js : Passing an external variable to a component through initialization? - Stack Overflow

I am trying to pass a variable (here, externalVar) to a ponent, directly when initializing. But I can&#

I am trying to pass a variable (here, externalVar) to a ponent, directly when initializing. But I can't find how to do it (probably not understanding documentation well :/ ). What is the correct way to do it?

The initialization :

var externalVar = "hello world"

const leftmenu = new Vue({
    el: "#left-menu",
    template: "<CLM/>",
    ponents: {CLM},
    variableToPass: externalVar
});

The ponent I am initializing here is defined like this (getting back variableToPass in data):

<template src="./template-l-m.html"></template>

<script>
import draggable from 'vuedraggable';
export default {
    name: 'leftmenu',
    ponents: {
        draggable
    },

    data () {
        return {
            jsonObject: this.variableToPass,
        }
    },
[ ... ]
</script>

But then , when I am trying to use this.jsonObject, it says that it's undefined. What am I doing wrong ?

I am trying to pass a variable (here, externalVar) to a ponent, directly when initializing. But I can't find how to do it (probably not understanding documentation well :/ ). What is the correct way to do it?

The initialization :

var externalVar = "hello world"

const leftmenu = new Vue({
    el: "#left-menu",
    template: "<CLM/>",
    ponents: {CLM},
    variableToPass: externalVar
});

The ponent I am initializing here is defined like this (getting back variableToPass in data):

<template src="./template-l-m.html"></template>

<script>
import draggable from 'vuedraggable';
export default {
    name: 'leftmenu',
    ponents: {
        draggable
    },

    data () {
        return {
            jsonObject: this.variableToPass,
        }
    },
[ ... ]
</script>

But then , when I am trying to use this.jsonObject, it says that it's undefined. What am I doing wrong ?

Share Improve this question asked Jul 28, 2017 at 9:57 Bénédicte LagougeBénédicte Lagouge 4922 gold badges9 silver badges25 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

If i understand you correctly you want to use props to pass data to child ponents

Dynamically bind a prop attribute on child ponent element using :variable="variableToPass"

var externalVar = "hello world"

const leftmenu = new Vue({
    el: "#left-menu",
    template: "<CLM :variable='variableToPass'/>",
    ponents: {CLM},
    data: {
        variableToPass: externalVar
    }
    
});

Define a props option in the child ponent

<template src="./template-l-m.html"></template>

<script>
import draggable from 'vuedraggable';
export default {
    name: 'leftmenu',
    ponents: {
        draggable
    },
    props: ['variable'],
    data () {
        return {
            jsonObject: this.variable,
        }
    },
[ ... ]
</script> 

Use data.

var externalVar = "hello world"

const leftmenu = new Vue({
    el: "#left-menu",
    template: "<CLM/>",
    ponents: {CLM},
    data: {
        variableToPass: externalVar
    }
});

That way you can access your variable like this this.$data.variableToPass

Use $options in child ponent

  mounted() {
    console.log(this.$parent.$options.variableToPass) // hello world
    this.jsonObject = this.$parent.$options.variableToPass
  }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745453844a4628393.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信