4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Vue-CLI3でjQueryを使う

Last updated at Posted at 2019-06-07

最近「jQueryいらない」みたいな風潮ですが、
「bodyにclass追加したい」と思ったら、jQueryを使うのが速い。
使い分け大事

$ npm install --save-dev jquery

Vue-CLI-uiを使うとさらに簡単です。
スクリーンショット 2019-06-07 18.44.11.png

App.vueに読み込む

App.vue
<template>
  <div id="app">
    <router-view />
  </div>
</template>
<script>
import $ from 'jquery' // ← 読み込む
export default {
  //(省略)
}
if(navigator.userAgent.indexOf('Android')>0){
  $("body").addClass("Android"); // ← 書ける
};
</script>

スクリーンショット 2019-06-07 18.51.40.png

コレでAndroid用のCSSが書ける。

【追記】
あれ、ココに書いてDOMの操作できるっけ ??
mounted(){}の中に書いたほうが良いと思う。

App.vue
<script>
import $ from 'jquery' // ← 読み込む
export default {
  //(省略)
  mounted(){
    if(navigator.userAgent.indexOf('Android')>0){
      $("body").addClass("Android"); // ← 書ける
    };
  },
}
</script>
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?