[英]How to integrate bootstrap-tour to angular app?
Im trying to use the bootstrap-tour to my angular app so i´ve added it to my bundle: 我试图在我的角度应用程序中使用bootstrap-tour ,所以我已将其添加到我的捆绑包中:
//css
"../node_modules/bootstrap-tour/build/css/bootstrap-tour-standalone.css"
//js
"../node_modules/bootstrap-tour/build/js/bootstrap-tour-standalone.js"
Then in my component i tried to make a simple tour: 然后在我的组件中,我尝试进行一个简单的浏览:
ngAfterViewInit() {
let tour = new Tour({
name:'tour',
template: `<div>
<h3>Test</h3>
</div>`,
steps: [
{
element: "#test",
title: "Title of my step",
content: "Content of my step"
}
]
});
tour.init();
tour.start();
}
But on my console i get the error: vendor.bundle.js:149550 ERROR TypeError: Cannot read property 'backdrop' of undefined at Tour._showPopoverAndOverlay 但是在我的控制台上,我得到以下错误: vendor.bundle.js:149550错误TypeError:无法在Tour._showPopoverAndOverlay读取未定义的属性“背景”
Any ideas on how to solve it? 关于如何解决的任何想法?
The problem you are having is that Tour class, which is coming from the bootstrap-tour package, is not known to typescript. 您遇到的问题是来自bootstrap-tour包的Tour类对于打字稿而言是未知的。 What you have to do:
您要做的是:
Inside the component, 在组件内部,
declare var Tour: any;
Then the ngAfterViewInit() part, do the initialization: 然后ngAfterViewInit()部分,进行初始化:
this._ngZone.runOutsideAngular(() => {
this.tour = new Tour({
debug: true,
storage: false,
backdrop: true,
});
this.tour.addStep({
element: ".tour-nav-1",
title: "Title of my step 1",
content: "Content of my step 1",
placement: 'bottom',
backdrop: true,
});
this.tour.addStep({
element: ".tour-nav-2",
title: "Title of my step 2",
content: "Content of my step 2",
placement: 'bottom',
});
// Initialize the tour
this.tour.init();
});
and voila, you're all set. 瞧,您都准备好了。 Note that the this._ngZone is the ng zone which you can instantiate inside the constructor of the component like this:
请注意,this._ngZone是ng区域,您可以在组件的构造函数中实例化如下:
constructor(private _ngZone: NgZone) {}
You can then run the 然后,您可以运行
this.tour.start(true);
command anywhere inside the component to start the tour, just make sure the this.tour.init() is called before the this.tour.start() . 在组件内的任何位置执行命令以开始浏览,只需确保在this.tour.start()之前调用this.tour.init()即可。
Note that the this.tour is a component class variable that is declared as: 请注意,this.tour是一个组件类变量,声明为:
tour: any;
I have tested this to be working fine with bootstrap-tour version 0.12.0 and angular version 6.0.0. 我已经测试了它与Bootstrap-tour版本0.12.0和angular版本6.0.0的配合是否正常。 It should work just fine for any angular versions > 2
对于任何大于2的角版本,它应该都可以正常工作
If you have any questions, just ask. 如果你有问题,就问吧。
Niz is correct but one more important note to add is NOT to import the bootstrap-standalone file into the component. Niz是正确的,但是要添加的一个更重要的注意事项是不要将Bootstrap独立文件导入到组件中。 I was doing that and putting it in the bundle in the
.angular-cli.json
file as well and it wasn't working correctly. 我正在这样做,并将其放在
.angular-cli.json
文件中的捆绑包中,并且工作不正常。
I'm guessing that since it found a reference for Tour inside the component then the 我猜是因为它在组件内部找到了Tour的参考,所以
this._ngZone.runOutsideAngular
wasn't really doing anything. 真的什么也没做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.