Vue.js: 生成渲染函数失败

Vue.js: Failed to generate render function

我尝试 运行 一个简单的 Vue.js,但我不断收到以下错误:

[Vue warn]: It seems you are using the standalone build of Vue.js in an environment with Content Security Policy that prohibits unsafe-eval. The template compiler cannot work in this environment. Consider relaxing the policy to allow unsafe-eval or pre-compiling your templates into render functions.

main.js:3180 [Vue warn]: Failed to generate render function: EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "default-src 'self'". in

with(this){return _c('div',{attrs:{"id":"app"}})}

(found in )

但是,我无法理解导致此错误的原因。我似乎没有使用任何动态模板,所有内容都应该预编译。这是我的应用程序代码:

import Vue from 'vue'
import VueRouter from 'vue-router'

const Home = Vue.component('home-component', {
  render() {
    return <div>
      <router-link to="/users">Users</router-link>
      <router-link to="/about">About</router-link>
    </div>
  }
})

const Users = Vue.component('users-component', {
  render() {
    return <p>Users</p>
  }
})

const NotFound = Vue.component('not-found-component', {
  render() {
    return <p>Not Found</p>
  }
})

const routes = [
  { path: '/', component: Home },
  { path: '/users', component: Users }
]

const router = new VueRouter({
  routes
})

const app = new Vue({
  router
}).$mount('#app')

下面是我在 gulpfile 中处理 JavaScript 个文件的方式:

const paths = {
  main: 'web/javascript/src/main.js',
  allJs: 'web/javascript/**/*.{js,vue}',
  resultJs: 'public/assets/javascript/main.js',
};

gulp.task('scripts', function() {

  return browserify(paths.main)
    .transform(babelify, { presets: ['es2015'], plugins: ["transform-runtime"] })
    .transform(vueify)
    .bundle()
    .pipe(fs.createWriteStream(paths.resultJs))
}

Whosebug 上似乎有很多类似的问题,但 none 个对我有帮助。

浏览器抛出内容安全策略 (CSP) 错误,要解决此问题,您应该考虑切换到完全符合 CSP 的仅运行时版本。 https://vuejs.org/v2/guide/installation.html#CSP-environments

此外,您 return 写错了。它应该 return 一对括号 () 中的 jsx。下面的代码应该有所帮助

import Vue from 'vue'
import VueRouter from 'vue-router'

const Home = Vue.component('home-component', {
  render(h) {
    return(
    <div>
      <router-link to="/users">Users</router-link>
      <router-link to="/about">About</router-link>
    </div>
    )
  }
})

const Users = Vue.component('users-component', {
  render(h) {
    return(
      <p>Users</p>
    )
  }
})

const NotFound = Vue.component('not-found-component', {
  render(h) {
    return(
      <p>Not Found</p>
    )
  }
})

const routes = [
  { path: '/', component: Home },
  { path: '/users', component: Users }
]

const router = new VueRouter({
  routes
})

const app = new Vue({
  router
}).$mount('#app')