admin管理员组文章数量:1356582
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 badges3 Answers
Reset to default 6import { 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);
});
}
};
本文标签: javascriptCannot import Popperjs in VueJSStack Overflow
版权声明:本文标题:javascript - Cannot import Popper.js in VueJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743977826a2570948.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论