您好我有这个架构(称为schema.js):
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var RoomSchema = new Schema({
name: { type: String, required: true, index: { unique: true } },
people: { type: Number, required: true },
childrens: {type: Number, required: true},
total: {type: Number, required: true}
});
var Room = mongoose.model('Room', RoomSchema);
var AvSchema = new Schema({
roomId: {type: Schema.Types.ObjectId, ref: 'Room'},
people: { type: Number, required: true },
childrens: {type: Number, required: true},
total: {type: Number, required: true}
});
var Av = mongoose.model('Av', …Run Code Online (Sandbox Code Playgroud) 我已经使用Angular 2/4大约一年了,我一直在回到这个困境,无论是否将路由器注入服务都被认为是一种不好的做法?
这是更具建筑性的问题.我相信没有确切的答案,但我想听听你的意见.所以这里有两个例子.
component.ts
constructor(private router: Router) {}
someAction() {
// Some code here
this.router.navigate(['/grid']);
}
Run Code Online (Sandbox Code Playgroud)
在这里我认为使用Router非常好,因为路由器和组件都是UI层.
auth.service.ts它并且它负责身份验证.我们希望能够将用户退出应用程序,我们有logout()能力这样做.auth.service.ts
constructor(private router: Router) {}
logout() {
// Cleanup token, storage, etc.
this.router.navigate(['/login']);
}
Run Code Online (Sandbox Code Playgroud)
所以在架构上思考:
我正在考虑在app.component.ts中eventEmitter加入authService并订阅它,但仍然不确定它是否比在服务中使用它更好.
我感谢对此案的任何评论.非常感谢!
编辑
另一个例子:UI是一个包含任务的日历.
有一种服务可以处理所有数据流并为日历提供数据.日历本身不会询问数据,而是订阅服务中的数据更改.
现在我需要将用户路由到此日历的不同屏幕.想象一下,用户点击下周/月/年.
此数据存储在路径URL中,因此用户可以在页面刷新后的同一天停留,但日历组件不知道日期/周/月.
它们包含在服务中.那么在这种情况下你会使用路由器吗?
我有这样的数据结构:
[
"category1": {
"name": "Cat 1",
"competences": [{
"name": "Comp 1",
"users": [{
"name": "user1",
"level": 99,
}, {
"name": "user2",
"level": 99,
},… {
"name": "userN",
"level": 9001,
}]
}, {
"name": "Comp 2",
"users": [{
"name": "user1",
"level": 99,
}, {
"name": "user2",
"level": 99,
},… {
"name": "userN",
"level": 9001,
}]
}]
},
"category2": {
"name": "Cat 2",
"competences": [{
"name": "Comp 3",
"users": [{
"name": "user1",
"level": 99,
}, {
"name": "user2",
"level": 99,
},… …Run Code Online (Sandbox Code Playgroud) 我刚刚安装了vue-instant来为搜索提供自动建议,并获得这样的示例代码
https://jsfiddle.net/santiblanko/dqo6vr57/
我的问题是如何将组件移动'vue-instant': VueInstant.VueInstant到像这样的新Vue组件:
Vue.component('vue-instant-component', {
//vue-instant
}
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的事情:
Vue.component('vue-instant', {
data: {
value: '',
suggestionAttribute: 'original_title',
suggestions: [],
selectedEvent: ""
},
methods: {
clickInput: function() {
this.selectedEvent = 'click input'
},
clickButton: function() {
this.selectedEvent = 'click button'
},
selected: function() {
this.selectedEvent = 'selection changed'
},
enter: function() {
this.selectedEvent = 'enter'
},
keyUp: function() {
this.selectedEvent = 'keyup pressed'
},
keyDown: function() {
this.selectedEvent = 'keyDown pressed'
},
keyRight: function() {
this.selectedEvent = 'keyRight pressed' …Run Code Online (Sandbox Code Playgroud) 我正在开发一个项目,该项目需要迁移旧 Http 的所有实例以利用 Angular 提供的新 HttpClient。异步方法在 DataService 类中声明如下:
@Injectable()
export class DataService {
constructor(private http: Http) { }
async getAsync<T>(resource: string, options?: RequestOptions): Promise<T> {
return this.http
.get(resource, options)
.map((response: Response) => response.json())
.catch(this.handleError).toPromise();
}
Run Code Online (Sandbox Code Playgroud)
并在另一个服务类中使用,如下所示:
async getUser() {
try {
this.logger.debug('Getting User State...');
const userInfo = await this.dataService.getAsync<[Type Parameter]>(
this.constantService.urls.authenticatedUserUri, <RequestOptions>{ withCredentials: true }
);
this.logger.debug(JSON.stringify(userInfo));
this.authorizeUser(userInfo);
} catch (error) {
this.logger.debug(error);
}
}
Run Code Online (Sandbox Code Playgroud)
我知道为了使用 HttpClient,我需要在提供 DataService 类的模块中导入 HttpClientModule,然后在服务本身中导入 HttpClient 并通过类的构造函数注入它。我也知道该行.map((response: Response) => response.json() )是不必要的,因为 JSON …
我在组件中有这段代码
<a *ngFor="let item of config.socialLinks" [routerLink]="[item.url]">
...
</a>
Run Code Online (Sandbox Code Playgroud)
当使用app.component中定义的本地路由但是当外部路由重定向到http:localhost:5555时,这可以正常工作.http://external.com 有没有办法将routerLink用于本地路由,并将href用于外部路由?
我有'工作'和'用户'集合.每个用户都可以为给定的'jobCategoryId'创建一个作业,然后该作业将保存在'jobs'集合中,并包含其创建者和'jobCategoryId'的'userId'.
我正在尝试将这两个集合组合在一起,因此当我获取作业时,我将拥有所有用户数据的"用户"字段,而不是该作业的"userId"字段.这就是我现在这样做的方式:
Job.aggregate([{
$match: {
jobCategoryId: mongoose.Types.ObjectId(jobCategoryId)
}
}, {
$lookup: {
from: 'users',
localField: 'userId',
foreignField: '_id',
as: 'user'
}
}, {
$project: {
_id: 1,
description: 1,
title: 1,
user: { $arrayElemAt: ['$user', 0] }
}
}, {
$project: {
_id: 1,
title: 1,
description: 1,
'user.name': '$user.name'
}
}])
Run Code Online (Sandbox Code Playgroud)
返回以下内容:
[{
_id: 'some id',
title: 'title',
description: 'description',
user: {
name: 'Mike'
}
}]
Run Code Online (Sandbox Code Playgroud)
结果是正确的但我真的很感兴趣,如果有任何其他方法在使用$ arrayElemAt时直接保留用户对象的某些字段,或者我注定要在管道中使用2个投影?有谁可以帮我解决这个小问题?我真的很感激.
在这个函数中,每个东西都可以正常工作,但是this.nav.popToRoot()在该位置不起作用.如果我将它移动到函数的开头,它可以正常工作.没有人对此有合理的解释.
提前感谢您的时间和考虑.
这是booking.ts组件中的代码:
book(){
let newReservation = {
_id: this.room._id,
from: this.details.from.substring(0,10),
to: this.details.to.substring(0,10)
}
let loading = this.loadingCtrl.create({
content: "Booking room..."
});
loading.present();
this.roomsService.reserveRoom(newReservation).then((res) => {
loading.dismiss();
console.log("Room reserved successfully ... ");
this.nav.popToRoot();
}, (err) => {
console.log(err);
});
}
Run Code Online (Sandbox Code Playgroud) angular ×4
mongoose ×2
express ×1
grid-layout ×1
http ×1
httpclient ×1
ionic2 ×1
javascript ×1
mongodb ×1
node.js ×1
vue.js ×1
vuejs2 ×1