简体   繁体   English

要求不能在vue.js中工作

[英]Require not working in vue.js

I'm new to vue trying to build a simple SPA with Vue without vue-router . 我是vue的新手,它尝试使用不带vue-router Vue构建简单的SPA。 Following the vue-2.0-simple-routing-example I'm just trying to serve pages via require(dynamicPathToFile+'.vue') . vue-2.0-simple-routing-example我只是尝试通过require(dynamicPathToFile+'.vue')服务页面。 But it's not working. 但这不起作用。

This works: 这有效:

Main.Vue Main.Vue

<template>
    <div>
        <p>Hello World</p>
    </div>
</template>

Main.JS Main.JS

import Vue from 'vue'
import App from './main.vue'

const app = new Vue({
  el: '#app',
  render (h) {
    return h(App)
  }
})

but why doesn't this work? 但是为什么不起作用呢?

Main.JS Main.JS

import Vue from 'vue'

const app = new Vue({
  el: '#app',
  render (h) {
    return h(require('./main.vue'))
  }
})

It compiles just fine, but in my javascript console I get 它可以编译,但是在我的JavaScript控制台中

[Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <Anonymous>
       <Root>

Nothing displays 什么都没显示

how do i get it to work so I can dynamically require a page, rather than import it at the top? 我如何使其工作,以便我可以动态地需要一个页面,而不是将其导入顶部?

需要包含.default,因为我正在使用Babel的更新(固定)版本

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

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