Vue中使用JQuery

安裝jQuery依賴

npm install jquery --save

或者用淘寶鏡像cnpm

cnpm install jquery --save

webpack.base.conf配置文件相關添加如下圖

const webpack = require('webpack')

// 添加代碼
plugins: [
    new webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery",
        jquery: "jquery",
        "window.jQuery": "jquery"
    })
],

在這裏插入圖片描述
在組件中引入jquery

import $ from "jquery";

整個HelloWorld.vue

<template>
  <div class="hello">
    <div id="test">{{msg}}</div>
  </div>
</template>

<script>
import $ from "jquery";
export default {
  name: "HelloWorld",
  data() {
    return {
      msg: "你好啊JQuery!"
    };
  },
  methods: {},
  mounted() {
    console.log($("#test"))
    $("#test").css({"background":'#0F0'})
  },
  created() {}
};
</script>

<style scoped>
</style>

效果圖
在這裏插入圖片描述

發佈了37 篇原創文章 · 獲贊 9 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章