我的棱角项目上有一条隐秘的错误消息
Error handling response: TypeError: Cannot read property 'length' of undefined at setTitle
错误消息导致此错误
content_script.js
function setTitle(result) {
if(result[document.URL]) {
document.title = result[document.URL];
}
else {
var regex_list = result["regex"];
for (i = 0; i < regex_list.length; i += 2) {
if (document.URL.match(regex_list[i])) {
document.title = regex_list[i+1];
}
}
}
}
function onError(error) {
console.log(`Error: ${error}`);
}
function init() {
chrome.storage.sync.get(null, setTitle);
}
init();
我没有创建content_script.js,我想这是在构建项目时生成的东西
我项目中唯一使用setTitle的地方是app.component.ts
import { Component } from "@angular/core";
import { Title } from "@angular/platform-browser";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
title: string = "MY TITLE";
public constructor(private titleService: Title) {}
ngOnInit() {
this.titleService.setTitle(this.title);
}
}
如果我删除ngOnInit内容,错误仍然出现
编辑: setTitle导致这段代码
title.d.ts
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* Factory to create Title service.
*/
export declare function createTitle(): Title;
/**
* A service that can be used to get and set the title of a current HTML document.
*
* Since an Angular application can't be bootstrapped on the entire HTML document (`<html>` tag)
* it is not possible to bind to the `text` property of the `HTMLTitleElement` elements
* (representing the `<title>` tag). Instead, this service can be used to set and get the current
* title value.
*
* @publicApi
*/
export declare class Title {
private _doc;
constructor(_doc: any);
/**
* Get the title of the current HTML document.
*/
getTitle(): string;
/**
* Set the title of the current HTML document.
* @param newTitle
*/
setTitle(newTitle: string): void;
}
答案 0 :(得分:1)
您的数组似乎为空:
var regex_list = result["regex"];
尝试在运行循环语句之前添加数组检查:
var regex_list = result["regex"];
if (regex_list && regex_list.length) {
for (i = 0; i < regex_list.length; i += 2) {
if (document.URL.match(regex_list[i])) {
document.title = regex_list[i+1];
}
}
}
答案 1 :(得分:0)
没关系,只需要清除我的缓存即可。错误不再发生了,对不起大家