javascript - Vuejs 不从 mixins 中读取属性并导出 NOT FOUND 错误。

标签 javascript webpack vue.js vuejs2 babeljs

我已经在这个错误上待了 3 天了,不确定这是不是一个错误,但我从 vuejs/vue-cli 安装了 vuejs

使用以下说明:

npm install -g vue-cli 
vue init webpack foo.com
cd foo.com
npm install
npm run dev

到目前为止一切正常,现在我在 src/

中创建了一个这样的目录结构
— src
   — assets
   — filters
   — config
   — components
       Profiles.vue
       Login.vue
   profiles.js
   routes.js
   main.js

所以,在 routes.js 中,我有这样的东西。

// routes.js
import ProfilesView from './components/Profiles.vue'
import LoginView from './components/Login.vue'

const routes = [{
  path: '/profiles',
  component: ProfilesView 
},
{
  path: '/login',
  component: LogoutView
}]

export default routes

到目前为止,我对上面的代码没有任何问题,问题来自以下两个文件 profiles.jsProfiles.vue

这是profiles.js

const profiles = Vue.mixin({
  data: function () {
    return { profiles: [] }
  },
  methods: {
   fetchProfiles: function(){....},
   mounted: function(){ this.fetchProfiles() }
})

export default profiles

这里是 Profiles.vue

<template lang="html">
  <div> {{ profiles }}<div>
</template>

<script>
  import { profiles } from '../../profiles'
  export default {
    mixins: [profiles],
    data () {
      return { profiles: [] }
    },
    mounted () {}
  }
</script>

<style lang="css">
</style>

使用上面的代码,我得到了这些错误。

profiles.js:1 Uncaught ReferenceError: Vue is not defined
    at Object.<anonymous> (profiles.js:1)
    at __webpack_require__ (bootstrap 1995fd0fa58cb1432d6f:659)
    at fn (bootstrap 1995fd0fa58cb1432d6f:85)
    at Object.defineProperty.value (app.js:55806)
    at __webpack_require__ (bootstrap 1995fd0fa58cb1432d6f:659)
    at fn (bootstrap 1995fd0fa58cb1432d6f:85)
    at Object.<anonymous> (Profiles.vue:7)
    at __webpack_require__ (bootstrap 1995fd0fa58cb1432d6f:659)
    at fn (bootstrap 1995fd0fa58cb1432d6f:85)
    at Object.__webpack_exports__.a (app.js:56033)

如果我将 import Vue from 'vue' 添加到 profiles.js 上面的错误将被替换为这个错误:

Uncaught TypeError: Cannot read property 'components' of undefined
    at checkComponents (vue.esm.js:1282)
    at mergeOptions (vue.esm.js:1363)
    at mergeOptions (vue.esm.js:1379)
    at Function.Vue.extend (vue.esm.js:4401)
    at Object.exports.createRecord (index.js:47)
    at Profiles.vue:26
    at Object.<anonymous> (Profiles.vue:30)
    at __webpack_require__ (bootstrap 625917526b6fc2d04149:659)
    at fn (bootstrap 625917526b6fc2d04149:85)
    at Object.__webpack_exports__.a (app.js:56036)

这是在提示 profiles.js 中的 mixins: [profiles],,我认为 profiles prop 是未定义的,但是事实并非如此。出于某种原因,Profiles.vue 没有从 profiles.js 读取或读取正确的数据,因为我也总是收到此错误:

[HMR] bundle has 1 warnings
client.js:161 ./~/babel-loader/lib!./~/vue-loader/lib/selector.js?type=script&index=0!./src/components/Profiles.vue
6:11-15 "export 'profiles' was not found in '../../profiles'

它提示 export default profiles 没有在 profiles.js 中找到,尽管它显然存在。我什至尝试使用 require,改变路径……一切。什么都不管用。

如有任何帮助,我将不胜感激。

最佳答案

您正在以错误的方式导入 profiles.js:

import { profiles } from '../../profiles'

因为你使用的是export default,所以应该是

import profiles from '../../profiles'

关于javascript - Vuejs 不从 mixins 中读取属性并导出 NOT FOUND 错误。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45878277/

相关文章:

javascript - 如何在 JavaScript 中获取附加的 accodian

vue.js - vuejs 将数据传递给模板 html 属性

npm - webpack 4 是否重复删除了已经被 webpack 捆绑的 npm 包?

javascript - 如何将 Laravel 从本地主机更改为实时 IP 地址?

javascript - Vue.js:使用 axios.get 之后的方法返回图像

javascript - Bootstrap3 两个下拉菜单在同一行

javascript - 加速我的 Javascript 滚动事件代码

javascript - 想要删除以前添加的类并将新类添加到同一 div

webpack - 如何将 jointjs 与 Vue 2 和 webpack 集成

node.js - 在 Node 中提供 gzip 文件会在浏览器中抛出 net::ERR_CONTENT_DECODING_FAILED