简体   繁体   English

VueJS在另一个组件内使用组件的麻烦

[英]VueJS trouble with using component inside another component

I'm new to VueJS and have already spent like 30 hours on learning it. 我是VueJS的新手,已经花了30个小时来学习它。 I have now trouble with using component inside another component. 我现在在另一个组件内部使用组件时遇到麻烦。 I may need someone who can help me to explain a little bit. 我可能需要有人可以帮助我解释一下。

before the quesion, let me make something clear: 在提出问题之前,让我澄清一下:

  1. According to the Vue JS officail site: 根据Vue JS官方网站的说法:

We do not recommend that beginners start with vue-cli, especially if you are not yet familiar with Node.js-based build tools. 我们不建议初学者从vue-cli开始,尤其是如果您还不熟悉基于Node.js的构建工具时。

I'm pretty new with JS-Framework so I chose to download the Vue.js and bind it to my HTML file. 我是JS-Framework的新手,所以我选择下载Vue.js并将其绑定到我的HTML文件。

  1. I only have html, js and css 我只有html,js和CSS

For those who don't what to read through all the codes, I will simplify my quesion here. 对于那些不懂所有代码的人,我将在这里简化我的提问。 Because I think when someone is very experienced, he/she may not need to read my silly codes: 因为我认为当某人非常有经验时,他/她可能不需要阅读我的愚蠢代码:

basiclly I have defined two global components, let's say component1 and component2. 基本上,我定义了两个全局组件,比如component1和component2。 And in HTML, I use the component1 like this to get multiple divs automaticlly built with the JSON data: 在HTML中,我使用像这样的component1来获取使用JSON数据自动构建的多个div:

<div v-for="data in JSON">
    <component1 v-bind:datainfo="data"></component1>
</div>

And then, I use the component2 inside the component1's template like this: 然后,我像这样在component1的模板中使用component2:

template:`
    <div v-for="somedata in JSON">
        <component2 v-bind:datainfo2="data"></component2>
    </div>
`

finally I defined some methods in component2, there comes the question. 最后,我在component2中定义了一些方法,出现了问题。 All of them (the functions in methods in component2) won't be defined and I get the warning from Vue says: 所有这些(component2中方法中的函数)都不会被定义,我从Vue收到警告说:

[Vue warn]: Property or method "each function in my methods in component2" is not defined on the instance but referenced during render. [Vue警告]:属性或方法“ component2中我的方法中的每个函数”未在实例上定义,但在渲染期间被引用。

So I am wondering if we are allowed to put a component inside another component or should I do this in another way. 因此,我想知道是否允许我们将一个组件放置在另一个组件内,还是应该以其他方式进行。 But if we can use multiple components inside each other, why can't we defind some methods in the inside component, the data property works fine, but methods not. 但是,如果我们可以在内部使用多个组件,那么为什么不能在内部组件中定义一些方法,则data属性可以正常工作,但方法不能。

For those, who want to read the original codes to understand my question, I'll post them here: (forgive my poor English) 对于那些想要阅读原始代码以理解我的问题的人,我将其张贴在这里:(原谅我可怜的英语)

The trouble I'm dealing with is the methods in the inside component of another component won't be defined by Vue. 我要解决的问题是Vue不会定义另一个组件内部组件中的方法。

The related HTML Codes: 相关的HTML代码:

<div v-for="(layer, idx) in WMSLayersSource">
    <wms-layer-select-group v-bind:layergroupinfo="layer"></wms-layer-select-group>
</div>

The main.js file: main.js文件:

var wmsLayerSelectSingle = Vue.component('wms-layer-select-single', {
props:['singlelayerinfo'], // case-insensitive and don't use '-'
data: function() {
    return {
        opacitySingle: 'display: none',
    }
},
method: {
    layerClickSingle: function() {
        if (this.opacitySingle == 'display: block') {
            this.opacitySingle = 'display: none';
        } else {
            this.opacitySingle = 'display: block';
        };
    },
    test: function() {
        console.log('test');
    }
},
template: `
    <li class="singleLayer">
        <input type="checkbox" />
        <span v-on:click="layerClickSingle">
            {{ singlelayerinfo.layers }}
        </span>
        <input class="opacity" v-bind:style="opacitySingle" min="0" max="1" step="0.1" value="1.0" type="range">
    </li>
` 
});

var wmsLayerSelectGroup = Vue.component('wms-layer-select-group', {
props: ['layergroupinfo'], // case-insensitive and don't use '-'
data: function() {
    return {
        displaySingle: '',//'display: none',
        opacityGroup: '',
    }
},
methods: {
    layerClickGroup: function() {
        console.log('layerClickGroup ');
        if (this.displaySingle == 'display: block') {
            this.displaySingle = 'display: none';
        } else {
            this.displaySingle = 'display: block';
        };
    },
},
created: function() {
    console.log('Component wms-layer-select-group is created');
    // hide the sublayers of a layer group and show the single layers if they don't belong to a group
    if (this.layergroupinfo.groupName == "singleLayer") {
        this.displaySingle = 'display: block';
    } else {
        this.displaySingle = 'display: none';
    }
},
template: `
    <div>
        <li class="LayerGroup" v-if="layergroupinfo.groupName != 'singleLayer'">
            <input type="checkbox" />
            <span @click="layerClickGroup">
                <b>{{ layergroupinfo.groupName }}</b>
            </span>
        </li>
        <div v-for="(singleLayer, idx) in layergroupinfo.layerCollection" v-bind:style="displaySingle">
            <wms-layer-select-single v-bind:singlelayerinfo="singleLayer"></wms-layer-select-single>
        </div>
        <hr/>
    </div>
`
});

I'm very careful with things like typo and case-insensitivity and double checked everything. 我对错别字和不区分大小写的内容非常小心,并仔细检查了所有内容。 because the warning by Vue I got is only the 因为我收到的Vue的警告只是

[Vue warn]: Property or method "layerClickSingle" is not defined on the instance but referenced during render. [警告]:属性或方法“ layerClickSingle”未在实例上定义,但在渲染期间被引用。

and everything else works fine. 其他一切正常。 So I would like to know why the method "layerClickSingle" in the inside component won't work. 因此,我想知道内部组件中的方法“ layerClickSingle”为什么不起作用。

method: {
layerClickSingle: function() {
    if (this.opacitySingle == 'display: block') {
        this.opacitySingle = 'display: none';
    } else {
        this.opacitySingle = 'display: block';
    };
},
test: function() {
    console.log('test');
}
},

This should be methods not method 这应该是methods而不是method

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM