对于想了解js控制台显示[objectObject]问题的读者,本文将是一篇不可错过的文章,我们将详细介绍js向控制台输出信息,并且为您提供关于angular–无法解析AuthenticationSe
对于想了解js控制台显示[object Object]问题的读者,本文将是一篇不可错过的文章,我们将详细介绍js向控制台输出信息,并且为您提供关于angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])、ASP.NET list的有价值信息。
本文目录一览:- js控制台显示[object Object]问题(js向控制台输出信息)
- angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])
- ASP.NET list
- Blob另存为[object Object] Node.js
- c – delete(Object)等效于调用Object .~Object()
js控制台显示[object Object]问题(js向控制台输出信息)
问题描述:在console.log中打印一个json对象时会显示[object Object]
//清空控制台输出并将光标显示在第一排
console.clear();
//定义一个json格式的对象
var j = {};
j.name = "小J";
j.sex = true;
j.age = 19;
//控制台输出
console.log("JSON对象:"+j);
console.log("JSON对象:"+JSON.stringify(j));
console.log(j);
console.log("JSON对象:%o",j);
控制台显示
需要先了解Console对象
Console 对象用于 JavaScript 调试。JavaScript 原生中默认是没有 Console 对象,这是宿主对象(也就是游览器)提供的内置对象。 用于访问调试控制台, 在不同的浏览器里效果可能不同。
console.log()方法可以简单地传递一系列对象,这些对象的字符串表示形式被串联成一个字符串,然后输出到控制台。(以Object.prototype.toString()的方法表示为字符串)
正式因为console.log()在输出多个对象的时候会以字符串的形式串联成一个字符串输出,所以会显示为“JSON对象: [object Object]"。
要想正确显示一个json对象可以单独打印,或者利用JSON.stringify()方法现将要打印的json对象格式化为字符串。
当然console.log()支持字符串替换,所以也可以用字符串替换的方式
%o、%O:输出一个JavaScript对象,在控制台中可以打开有关对象的更多信息。%后面是对象Object的首写字母O
angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])
Can’t resolve all parameters for AuthenticationService: ([object Object],?,[object Object])
我已经检查了几乎每个主题,并尝试了多种方法来解决它,但仍然无法在第二天击败它.
我试图像这样在appService中注入第一个authService但是得到了同样的错误
@Inject(forwardRef(() => AuthenticationService)) public authService: AuthenticationService
我检查了所有DI和服务内部的导入顺序,在我看来一切都是正确的
如果有人可以帮我处理它,我很感激.
Angular 4.0.0
AuthService
import { Injectable } from '@angular/core'; import {Http,Headers,Response} from '@angular/http'; import 'rxjs/add/operator/toPromise'; import {Observable} from 'rxjs/Rx'; import {AppServices} from "../../app.services"; import {Router} from "@angular/router"; @Injectable() export class AuthenticationService { public token: any; constructor( private http: Http,private appService: AppServices,private router: Router ) { this.token = localStorage.getItem('token'); } login(username: string,password: string): Observable<boolean> { let headers = new Headers(); let body = null; headers.append("Authorization",("Basic " + btoa(username + ':' + password))); return this.http.post(this.appService.api + '/login',body,{headers: headers}) .map((response: Response) => { let token = response.json() && response.json().token; if (token) { this.token = token; localStorage.setItem('Conform_token',token); return true; } else { return false; } }); } logout(): void { this.token = null; localStorage.removeItem('Conform_token'); this.router.navigate(['/login']); } }
应用服务
import {Injectable} from '@angular/core'; import {Headers,Http,RequestOptions} from '@angular/http'; import {Router} from "@angular/router"; import {AuthenticationService} from "./auth/auth.service"; import 'rxjs/add/operator/toPromise'; import {Observable} from 'rxjs/Rx'; @Injectable() export class AppServices { api = '//endpoint/'; public options: any; constructor( private http: Http,private router: Router,public authService: AuthenticationService // doesn't work // @Inject(forwardRef(() => AuthenticationService)) public authService: AuthenticationService // doesn't work either ) { let head = new Headers({ 'Authorization': 'Bearer ' + this.authService.token,"Content-Type": "application/json; charset=utf8" }); this.options = new RequestOptions({headers: head}); } // ==================== // data services // ==================== getData(): Promise<any> { return this.http .get(this.api + "/data",this.options) .toPromise() .then(response => response.json() as Array<Object>) .catch((err)=>{this.handleError(err);}) }
应用模块
import { browserModule } from '@angular/platform-browser'; import { browserAnimationsModule } from '@angular/platform-browser/animations'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import {BaseRequestOptions,HttpModule} from '@angular/http'; import { MaterialModule} from '@angular/material'; import {FlexLayoutModule} from "@angular/flex-layout"; import 'hammerjs'; import { routing,appRoutingProviders } from './app.routing'; import { AppServices } from './app.services'; import {AuthGuard} from "./auth/auth.guard"; import {AuthenticationService} from "./auth/auth.service"; import {AppComponent} from './app.component'; import {AuthComponent} from './auth/auth.component'; import {NotFoundComponent} from './404/not-found.component'; import { HomeComponent } from './home/home.component'; @NgModule({ declarations: [ AppComponent,AuthComponent,NotFoundComponent,HomeComponent ],imports: [ browserModule,browserAnimationsModule,FormsModule,HttpModule,routing,MaterialModule,FlexLayoutModule ],providers: [AppServices,AuthGuard,AuthenticationService],bootstrap: [AppComponent] }) export class AppModule { }
解决方法
你可以使用
export class AuthenticationService { public token: any; appService: AppServices; constructor( private http: Http,// private appService: AppServices,injector:Injector; private router: Router ) { setTimeout(() => this.appService = injector.get(AppServices)); this.token = localStorage.getItem('token'); }
另见DI with cyclic dependency with custom HTTP and ConfigService
要避免使用setTimeout,您还可以从AppService的构造函数中设置AuthenticationService.appService(或者相反)
ASP.NET list
public partial class 测试 : System.Web.UI.Page
{
static List<Item> allAnswer= new List<Item>();
protected void Page_Load(object sender, EventArgs e)
{
//首次加载
if (IsPostBack == false)
{
//不能使用将allAnswer中的元素全部删除,这样也会将session中的值清空
//allAnswer.clean();
//使用重新定义新的空的对象来实现对allAnswer的清空
allAnswer = new List<Item>();
List<Item> reallAnswer = null;
try
{
//其中Session["ReAllAnswer"]来自于另一页面
reallAnswer = (List<Item>)Session["ReAllAnswer"];
//PrintAllAnwser(reallAnswer);
}
catch { }
}
}
如果使用allAnswer.clean()函数,则接收的数据Session["ReAllAnswer"]将会设置为空;
而使用new List<Item>(),则不会。
Blob另存为[object Object] Node.js
我想用HTML5录制来自麦克风的音频,然后将其发送到服务器进行保存。但是,当前保存的文件仅包含[object Object]
这是我的代码片段。
前端:
console.log(blob);$http.post(''/api/save_recording'', blob) .success(function(new_recording) { console.log("success"); })
日志打印:
Blob {type: "audio/wav", size: 237612, slice: function}success
后端:
exports.saveRecording = function(req, res) { console.log(req.body); fs.writeFile("temp/test.wav", req.body, function(err) { if(err) { console.log("err", err); } else { return res.json({''status'': ''success''}); } }) }
日志打印: { type: ''audio/wav'', size: 786476 }
您能告诉我为什么它不起作用以及如何解决吗?
答案1
小编典典我终于完成了这项工作。使它起作用的方法是在客户端上对Blob进行编码,然后在服务器上对其进行解码。
前端:
// converts blob to base64var blobToBase64 = function(blob, cb) { var reader = new FileReader(); reader.onload = function() { var dataUrl = reader.result; var base64 = dataUrl.split('','')[1]; cb(base64); }; reader.readAsDataURL(blob);};blobToBase64(blob, function(base64){ // encode var update = {''blob'': base64}; $http.post(''/api/save_recording'', update) .success(function(new_recording) { console.log("success"); });});
后端:
exports.saveRecording = function(req, res) { var buf = new Buffer(req.body.blob, ''base64''); // decode fs.writeFile("temp/test.wav", buf, function(err) { if(err) { console.log("err", err); } else { return res.json({''status'': ''success''}); } }); };
c – delete(Object)等效于调用Object .~Object()
static void Constructor(ObjectType *thisPointer) { new(thisPointer) ObjectType(); }
和这样的代码来销毁对象:
static void Destructor(ObjectType *thisPointer) { thisPointer->~ObjectType(); }
我有几个问题:
>这种方式使用析构函数是否正确? (Eclipse判断这是一个错误)据我所知,这段代码应该调用析构函数而不释放内存(调用free())
>是否可以使用delete(thisPointer)(或类似的东西)而不是这种结构,它是否相同? (至少这段代码在编译和运行时没有错误)
>还有其他方法可以在不释放内存的情况下调用析构函数吗?
先感谢您.
解决方法
Is it correct way to use destructor this way?
是.您使用placement-new就地构造了对象,因此必须使用显式析构函数调用来销毁它(假设它具有非平凡的析构函数).
Is it possible to use
delete(thisPointer)
(or something like it) instead of this construction and is it will be equivalent?
删除号将尝试使用operator delete()将内存释放到免费存储区;这只有在使用普通的新表达式(或者可能是显式使用operator new())时才有效.
Is there other ways to call destructor without deallocating memory?
并不是的.调用析构函数当然是调用析构函数的最清晰,最简单的方法.
今天关于js控制台显示[object Object]问题和js向控制台输出信息的分享就到这里,希望大家有所收获,若想了解更多关于angular – 无法解析AuthenticationService的所有参数:([object Object],?,[object Object])、ASP.NET list等相关知识,可以在本站进行查询。
本文标签: