我试图在 javascript 函数内的应用程序组件中订阅一个可观察对象,它返回一个可观察对象,但是当我尝试在应用程序组件之前加载页面时出现下一个错误。
我需要这样做,因为我的应用程序有一部分是移动的,另一部分是网络的,因此用户有时会点击应用程序中的按钮返回
当用户点击背面按下时,我调用 javascript
class WebViewActivity : BaseActivity() {
lateinit var webView : WebView
@SuppressLint("SetJavaScriptEnabled", "MissingSuperCall")
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.AppTheme_NotView)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_webview)
webView = findViewById<WebView>(R.id.webview)
loadPage()
}
private fun loadPage() {
webView.clearCache(true)
webView.webViewClient = ActivityWebView(this)
webView.webChromeClient = ActivityWebViewChrome(this)
webView.addJavascriptInterface(BridgeJavaScript(this, webView), "Test")
webView.settings.javaScriptEnabled = true
webView.settings.domStorageEnabled = true
webView.loadUrl("http://localhost:9000/test")
}
override fun onBackPressed() {
webView.url?.let {
this.webView.evaluateJavascript("javascript:bacKPressed()",null);
}
}
}
当用户点击后退时,我调用了 javascript 函数,因此我需要打字稿在调用它时一直在监听事件。
<块引用>this.webView.evaluateJavascript("javascript:backPressed()",null);
Angular 版本是 8
assets 函数是下一个 .js
import {of} from "rxjs";
function bacKPressed() {
return of(true)
}
下一个是 angular.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"app": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/test",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": [
"src/assets/js/functions.js"
],
"es5BrowserSupport": true
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
}
]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "app:build"
},
"configurations": {
"production": {
"browserTarget": "app:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "app:build"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "app"
}
如果我删除了 javascript 函数中的 observable,就可以了,页面也加载了。
function bacKPressed() {
return true
}
接下来是打字稿代码
import { Observable, Subscription } from 'rxjs';
declare function bacKPressed(): Observable<boolean>;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
/**
* Clase inicial de la aplicación
*
* *Initial app class*
*
*/
export class AppComponent implements OnInit {
ngOnInit() {
bacKPressed().subscribe(()=> {
console.log('The onBackPressed was called');
});
}
}
错误是下一个
<块引用>未捕获的语法错误:不能在模块外使用导入语句 main.0dd98e5cd968fc0f00c4.js:2 致命。未找到外部上下文。 应用程序不可用。
如果我将导入更改为必需,它会显示下一个
const {of} = require("rxjs");
function bacKPressed() {
return of(true)
}
<块引用>
未捕获的 ReferenceError: require 未定义 在scripts.13f7030c5c675b4edf74.js:1