简体   繁体   English

在Vue.js中将Vueify用于仅运行时的组件

[英]Using Vueify for components with the runtime-only build in Vue.js

I've been working on porting a vue.js component from vue 1.0 to Vue 2.0 using Vueify. 我一直在使用Vueify将vue.js组件从vue 1.0移植到Vue 2.0。 In the Starter resources it states: 入门资源中,它指出:

When you use vue-loader or vueify to import *.vue files, their parts are automatically compiled into render functions. 当您使用vue-loader或vueify导入* .vue文件时,它们的部分将自动编译为渲染函数。 It is therefore recommended to use the runtime-only build with *.vue files. 因此,建议对* .vue文件使用仅运行时版本。

However, this does not appear to be the case. 但是,事实并非如此。 If I have a simple component like: 如果我有一个简单的组件,例如:

<template>
    <div>
        {{ msg }}
    </div>
</template>

<script type="text/javascript">
export default {
    props: {
        msg: {
            default: "Child Message"
        }
    }
}
</script>

And in my main.js file I do: 然后在我的main.js文件中:

import Vue from 'vue'
import MyComponent from './my-component.vue';

Vue.component('my-component', MyComponent);

new Vue({
    el: '#app',
    render: function(createElement) {
        return createElement(MyComponent)
    }
});

Then compile with Gulp using: 然后使用以下命令编译Gulp:

browserify('./main.js')
    .transform(vueify)
    .bundle()
    .pipe(fs.createWriteStream("bundle.js"))

I cannot do anything at all with the component except get it to render. 除了使它呈现之外,我对组件什么也不能做。 In fact, it will actually render the component as soon as it finds the div with the id "app": 实际上,它会在找到ID为“ app”的div时立即渲染该组件:

<div id="app">
  <!-- my-component renders even though I didn't ask it to -->
</div>

And any props added to the component are not received, so: 并且没有收到添加到组件的任何道具,因此:

    <div id="app">
      <!-- 
      Message displays as default "Child Message" rather than "Parent Message". The prop wasn't passed
      -->
      <my-component msg="Parent Message"></my-component>
    </div>

Similarly, if I add data to main.js , it's not accessible from the web page: 同样,如果我将data添加到main.js ,则无法从网页访问它:

import Vue from 'vue'
import MyComponent from './my-component.vue';

Vue.component('my-component', MyComponent);

new Vue({
    el: '#app',
    render: function(createElement) {
        return createElement(MyComponent)
    },
    data() {
        return {
            msg: "Parent Message"
        }
    }
});

in HTML: 在HTML中:

<div id="app">
  {{ msg }} // This will not print
</div>

And anything inside "#app" doesn't render at all (remember "my-component" is rendered even if I don't add it): 而且“ #app”中的任何内容都根本不会呈现(请记住,即使我不添加“ my-component”也会呈现):

<div id="app">
  <!-- This doesn't render-->
  <strong>A component </strong>
  <my-component></my-component>
</div>

So it looks to me like you can render a component, without any control over it, and you cant do anything further with the view model, so is it really the case that I should be using the runtime-only build as suggested? 因此在我看来,您可以呈现一个组件,而无需对其进行任何控制,并且您无法对视图模型做任何进一步的事情,是否真的应该按照建议使用仅运行时构建?

In the end I've used the standalone build using aliasify and everything works fine, but I'd really like to know what it is I am missing when using vueify with the runtime build. 最后,我使用了使用aliasify的独立构建,并且一切正常,但是我真的想知道在将vueify与运行时构建一起使用时缺少什么。 Surely the behavior I'm describing isn't what is supposed to happen, so I can only assume I have misunderstood something somewhere. 当然,我要描述的行为不是应该发生的事情,所以我只能假设我在某个地方误解了某些东西。

Doing some tests the problem is in your default render function: 做一些测试,问题出在您的默认渲染函数中:

render: function(createElement) {
    return createElement(MyComponent)
}

It's overriding the main Vue file's render function and creating a base MyComponent inserting it into the body. 它覆盖了主要的Vue文件的render函数,并创建了将其插入主体的基本MyComponent。

When I removed the render function the prop fired. 当我删除了渲染功能时,道具被触发了。

jsFiddle Try that, just uncomment the render function to see what I mean. jsFiddle尝试一下,只需取消注释render函数即可查看我的意思。

The render function is meant to replace html templating by allowing you to have more control and utilize other template options. 渲染功能旨在通过允许您进行更多控制并利用其他模板选项来替换html模板。

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

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