0

我是 vuejs 的新手。我正在使用 vue-cli 在 vue 项目中工作

  • 我安装了 vue vue/clinpm install -g @vue/cli
  • 我创建了一个项目vue init webpack my-project
  • 我安装了 jQuerynpm install jquery
  • 我安装了materialize-cssnpm install materialize-css
  • 之后一切都很好npm run dev
  • 但是当我这样做npm run buid并运行 dist 目录中的 index.html 文件时,我在 chrome devtools 中收到此错误
Uncaught TypeError: c(...)(...).modal is not a function
    at HTMLDocument.<anonymous> (App.vue:20)
    at l (jquery.js:3557)
    at d (jquery.js:3625)

我的 main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import vueResource from 'vue-resource'
import store from './components/store/AuthStore'
import 'materialize-css'
import 'materialize-css/dist/css/materialize.min.css'

let currentUrl = window.location.href
let url = new URL(currentUrl)
let tokenFromUrl = url.searchParams.get('token')
let nameFromUrl = url.searchParams.get('name')
let avatarFromUrl = url.searchParams.get('avatar_link')

if (tokenFromUrl) {
  localStorage.setItem('token', tokenFromUrl)
  localStorage.setItem('name', nameFromUrl)
  localStorage.setItem('avatar_link', avatarFromUrl)
}

let token = localStorage.getItem('token') ? localStorage.getItem('token') : ''

Vue.config.productionTip = false
Vue.prototype.$hostname = (Vue.config.productionTip) ? 'https://ipaq-mobile.000webhostapp.com/traitement' : 'http://127.0.0.1:8000'
Vue.prototype.$avatar_link = localStorage.getItem('avatar_link')
Vue.use(vueResource)
Vue.http.options.root = (Vue.config.productionTip) ? 'https://ipaq-mobile.000webhostapp.com/traitement/api' : 'http://127.0.0.1:8000/api'
Vue.http.headers.common['Access-Control-Allow-Headers'] = 'Origin, Accept, Authorisation'
Vue.http.headers.common['Accept'] = 'application/json'
Vue.http.headers.common['Authorization'] = 'Bearer ' + token

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

我的应用程序.vue

<template>
  <div id="app">
    <div class="navbar">
      <router-view name="navbar" v-bind:pathName="pathName"/>
          <!-- <div @click="loadMore = !loadMore"><h1><a>loadMore</a></h1></div> -->
    </div>
    <SideNav></SideNav>
    <router-view name="main"></router-view>
  </div>
</template>

<script>
import SideNav from './components/TheSideNav'
import $ from 'jquery'

export default {
  name: 'App',
  mounted: function () {
    $(document).ready(function () {
      $('.modal').modal()
      $('.sidenav').sidenav()
    })
  },
  components: {
    SideNav
  },
  computed: {
    pathName () {
      return this.$route.name
    }
  }
}

</script>

<style>

</style>

我怎样才能解决这个问题 ?

4

2 回答 2

0

你需要一个http服务器,比如nginx。

于 2020-03-21T04:18:25.820 回答
0

我的 build/webpack.dev.conf.js 中有这段代码

 new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
      'window.jQuery': 'jquery'
    })

这不在 build/webpack.prod.conf.js 中,所以我添加了它。

于 2020-03-31T07:38:31.867 回答