LoginSignup
5
10

More than 5 years have passed since last update.

[Vue warn]: Cannot find element

Last updated at Posted at 2016-07-09

Railsでvue.jsを使っています。Railsがjavascriptを一本化する方針のようなので、ID指定を行っていると[Vue warn]: Cannot find elementといった警告がよく出ます。

対応策は Instance Vue if element exists? などがあるようです。

ただ、コードを確認したところ https://github.com/vuejs/vue/search?utf8=%E2%9C%93&q=%22Cannot+find+element%22&type=Code によるとenv.NODE_ENVproductionなら本番では出ないようなので、開発用の警告と捉えて対応しなくてもいいのかもしれません。

IDが無い場合は読み込まないようにしてみました。

document.addEventListener('DOMContentLoaded', function() {
  if (document.getElementById("feed")) { require('./feed.js') }
  if (document.getElementById("shop")) { require('./shop.js')  }
}, false);
5
10
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
5
10