javascript - angular 5+ implement smart-app-banner vanilla JS
问题描述
I am trying to use smart-app-banner in my app, but since there are no typings on the web for it, I don't know how to use it within angular.
So far I've got this in my app.component.ts :
import * as smartBanner from "smart-app-banner";
let smartBannerInstance = smartBanner();
constructor() {
new smartBannerInstance({
daysHidden: 10, // days to hide banner after close button is clicked (defaults to 15)
daysReminder: 20, // days to hide banner after "VIEW" button is clicked (defaults to 90)
// appStoreLanguage: 'us', // language code for the App Store (defaults to user's browser language)
title: "Title",
author: "Authot",
button: "VIEW",
store: {
ios: "On the App Store",
android: "In Google Play"
},
price: {
ios: "FREE",
android: "FREE"
}
// force: 'android' // Uncomment for platform emulation
});
}
But I get the usual
Uncaught TypeError: Cannot set property 'options' of undefined at SmartBanner
How can I make this work?
解决方案
So, the whole deal with implementing a simple JS library into an Angular application goes like this (in this specific case that is, but it will work for most libraries):
First, we import the library by using the all selector as follows:
import * as SmartBanner from "../../node_modules/smart-app-banner/dist/smart-app-banner.js";
Then, we declare it inside our AppComponent class and then we initialize it within the constructor.
SmartBanner: any;
constructor() {
new SmartBanner({
daysHidden: 10, // days to hide banner after close button is clicked (defaults to 15)
daysReminder: 20, // days to hide banner after "VIEW" button is clicked (defaults to 90)
// appStoreLanguage: 'us', // language code for the App Store (defaults to user's browser language)
title: "Title",
author: "Author",
button: "VIEW",
store: {
ios: "On the App Store",
android: "In Google Play"
},
price: {
ios: "FREE",
android: "FREE"
}
// force: 'android' // Uncomment for platform emulation
});
}
And it works perfectly.
If this is somewhat of a performance issue, please anyone that has further knowledge, let me know.
推荐阅读
- java - AMQP - 异步 RPC - 返回值的期货导致“意外的返回值”
- jquery - jQuery isNumeric 问题
- docker - 即使在 pod 被删除后,kubelet 日志也会泛滥
- spring-integration - Spring Partition Batch 作业从 Spring Batch 2.2 迁移到 Spring Batch 4.02
- typescript - 如何对 JestJs 中 TypeScript 类的构造函数中抛出的异常进行单元测试
- kotlin - kotlin 如何共享常见的改造响应处理程序代码
- javascript - 打开另一个时如何隐藏一个引导弹出窗口?
- android - 在方向改变时在演示者中处理 Rx Disposable 的正确方法
- java - 编辑右键弹出约会JFXtras
- python - 如何将x个x长度的单词添加到不同的嵌套列表中