最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot import Popper.js in VueJS - Stack Overflow

programmeradmin2浏览0评论

I was following this tutorial in order to learn how to make popovers in my VueJS app. When I piled the projects I got an error that says:

[Vue warn]: Error in data(): "TypeError: _popperjs_core__WEBPACK_IMPORTED_MODULE_0__.default is undefined"

I traced the problem to the BasePopover.vue ponent - first <script> line that says import Popper from "popper.js"; In my application I changed that to import Popper from "@popperjs/core"; but the error still keeps appearing.

So I followed the official Popper.js tutorial to simplify things. I installed via npm i @popperjs/core (also tried using VueCLI as seen in the image below and via npm install @popperjs/core --save).

This is my current code:

<template>
  <div id="app">
    <button id="button" aria-describedby="tooltip">My button</button>
    <div id="tooltip" role="tooltip">My tooltip</div>
  </div>
</template>

<script>
//import Popper from "@popperjs/core/lib/popper";
import Popper from "@popperjs/core";
export default {
  name: "TestView",
  ponents: {
  },

  data() {
    
      const button = document.querySelector('#button');
      const tooltip = document.querySelector('#tooltip');

      Popper.createPopper(button, tooltip);
    
    return {
    };
  },
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  padding: 350px 0;
  background-color: #C4DFD1;
}
</style>

I don't know what else to do. Any help is appreciated!

I was following this tutorial in order to learn how to make popovers in my VueJS app. When I piled the projects I got an error that says:

[Vue warn]: Error in data(): "TypeError: _popperjs_core__WEBPACK_IMPORTED_MODULE_0__.default is undefined"

I traced the problem to the BasePopover.vue ponent - first <script> line that says import Popper from "popper.js"; In my application I changed that to import Popper from "@popperjs/core"; but the error still keeps appearing.

So I followed the official Popper.js tutorial to simplify things. I installed via npm i @popperjs/core (also tried using VueCLI as seen in the image below and via npm install @popperjs/core --save).

This is my current code:

<template>
  <div id="app">
    <button id="button" aria-describedby="tooltip">My button</button>
    <div id="tooltip" role="tooltip">My tooltip</div>
  </div>
</template>

<script>
//import Popper from "@popperjs/core/lib/popper";
import Popper from "@popperjs/core";
export default {
  name: "TestView",
  ponents: {
  },

  data() {
    
      const button = document.querySelector('#button');
      const tooltip = document.querySelector('#tooltip');

      Popper.createPopper(button, tooltip);
    
    return {
    };
  },
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  padding: 350px 0;
  background-color: #C4DFD1;
}
</style>

I don't know what else to do. Any help is appreciated!

Share Improve this question asked Sep 5, 2020 at 14:51 DoombringerDoombringer 6747 silver badges22 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6
import { createPopper } from '@popperjs/core';

export default {
name: "TestView",
ponents: {
},
data() {
return {
};
},
mounted(){
  const button = document.querySelector('#button');
  const tooltip = document.querySelector('#tooltip');
  createPopper(button, tooltip);
}
};

Instead of ids you should use refs(I have not used them here to avoid confusing you), which will ensure that there is no clash, since you app can have multiple elements with the same Id, eg #button. When using UI library like popper js, always try and put their code in the mounted hook, the mounted hook ensures that the elements you are targeting eg #button are in the dom

I think you should do this

import { createPopper } from '@popperjs/core';

and not as you have above i.e

import Popper from "@popperjs/core";

see here: module bundlers

You must add this.$nextTick for the code that will run only after the entire view has been rendered (https://vueponent./integrations/popperjs.html)

import { createPopper } from '@popperjs/core';
      
export default {
  name: "TestView",
  ponents: {
  },
  data() {
    return {
    };
  },
  mounted(){
    this.$nextTick(() => {
      const button = document.querySelector('#button');
      const tooltip = document.querySelector('#tooltip');
      createPopper(button, tooltip);
    });
  }
};
发布评论

评论列表(0)

  1. 暂无评论