Babel.js 无法将标记添加< script>到Vue组件文件(*.vue)

h5qlskok  于 2022-12-08  发布在  Babel
关注(0)|答案(1)|浏览(187)

我是vue.js的新手。
我试图渲染vue组件文件,但当我将<script></script>添加到vue组件时,例如app/javascript/packs/components/app.vue文件,babel-loader中发生编译错误。

错误日志

ERROR in ./node_modules/babel-loader/lib??ref--1-0!./node_modules/vue-loader/lib??vue-loader-options!./app/javascript/packs/app.vue?vue&type=script&lang=js
Module build failed: ReferenceError: [BABEL] /Users/shiho/dev/appname/app/javascript/packs/app.vue: Unknown option: base.omit. Check out http://babeljs.io/docs/usage/options/ for more information about options.

A common cause of this error is the presence of a configuration options object without the corresponding preset name. Example:

Invalid:
  `{ presets: [{option: value}] }`
Valid:
  `{ presets: [['presetName', {option: value}]] }`

For more detailed information on preset configuration, please see http://babeljs.io/docs/plugins/#pluginpresets-options.
    at Logger.error (/Users/shiho/dev/appname/node_modules/babel-core/lib/transformation/file/logger.js:41:11)
    at OptionManager.mergeOptions (/Users/shiho/dev/appname/node_modules/babel-core/lib/transformation/file/options/option-manager.js:226:20)
    at OptionManager.init (/Users/shiho/dev/appname/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at File.initOptions (/Users/shiho/dev/appname/node_modules/babel-core/lib/transformation/file/index.js:212:65)
    at new File (/Users/shiho/dev/appname/node_modules/babel-core/lib/transformation/file/index.js:135:24)
    at Pipeline.transform (/Users/shiho/dev/appname/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
    at transpile (/Users/shiho/dev/appname/node_modules/babel-loader/lib/index.js:50:20)
    at Object.module.exports (/Users/shiho/dev/appname/node_modules/babel-loader/lib/index.js:173:20)
 @ ./app/javascript/packs/app.vue?vue&type=script&lang=js 1:0-178 1:199-374
 @ ./app/javascript/packs/app.vue

.巴伯尔c

{

  "presets": [
    ["env", {
      "modules": false,
        "targets": {
          "browsers": "> 1%",
          "uglify": true
        },
        "useBuiltIns": true
    }]
  ],

  "plugins": [
    "syntax-dynamic-import",
    "transform-object-rest-spread",
    ["transform-class-properties", { "spec": true }]
  ]
}

装载机

% cat config/webpack/loaders/vue.js
const { dev_server: devServer } = require('@rails/webpacker').config

const isProduction = process.env.NODE_ENV === 'production'
const inDevServer = process.argv.find(v => v.includes('webpack-dev-server'))

module.exports = {
    test: /\.vue$/,
    loader: 'vue-loader',
    options: {
        loaders: {
          js: [
            { loader: 'babel-loader' }
          ]
        }
      }
}

配置/网络包/版本. js

const { dev_server: devServer } = require('@rails/webpacker').config

const isProduction = process.env.NODE_ENV === 'production'
const inDevServer = process.argv.find(v => v.includes('webpack-dev-server'))

module.exports = {
  test: /\.vue(\.erb)?$/,
  use: [{
    loader: 'vue-loader'
  }]
}

应用程序版本

<script>
export default {
  data: function () {
    return {
      message: "Hello Vue!"
    }
  }
}
</script>

甚至当我从.babelrc中删除presets时,也发生了同样的错误。

import Vue from 'vue/dist/vue.esm'
import InfiniteLoading from 'vue-infinite-loading';
import Item from './components/Item.vue'

window.onload = function() {

var highlightViewModel = new Vue({
    el: '#home-wrapper',
    data: {
        show: false,
        loading: false,
        message: '',
        items: [],
        message: 'helloooo',
    },
    beforeMount: function () {
        var that = this
        var params = {
            url: '/items/get_timeline.json',
            method: 'GET'
        }
        $.ajax(params).done(function(data){
            console.log(data.items);
            that.items =  data.items;
            Vue.nextTick(function () {
                $('.timeago').timeago();

            });
        });
    },
    components: {
        InfiniteLoading,
        Item
    },
    ...

应用程序/javascript/软件包/组件/项目.vue

<template>
  <div>
    <h3>aaa</h3>
  </div>
</template>

<!-- if I remove script tag below, error disappears and "aaa" rendered as much as items.count.-->
<script>
</script>

应用程序/视图/用户/home.html.erb

<item v-for="item in items" :article="item" :key="item.id"></item>
e0uiprwp

e0uiprwp1#

所以你想要一个只有脚本功能的.vue组件?不是很清楚。但是有很多不同的方法来注册组件和模板。
一个方法是Inline-Templates,这对这个很有用。你需要用这种方法注册<template>
https://v2.vuejs.org/v2/guide/components-edge-cases.html#Inline-Templates

相关问题