У меня есть домашнее приложение, относящееся к пользователю, но когда я нажимаю кнопку для go на странице addtodo
, оно теряет URL дома относительно пользователя, что затем приводит к todolist
хранится в Firebase как undefined
вместо дома, выбранного пользователем. Есть идеи, почему это так? Я попытался передать URL следующим образом.
Todolist.ts
import { Component, OnInit } from '@angular/core';
import {NavController} from "@ionic/angular";
import { House, HouseService } from '../Services/house.service';
import { ActivatedRoute, Route, Router } from '@angular/router';
import { TodoserviceService } from '../Services/todoservice.service';
import { AngularFirestore } from '@angular/fire/firestore';
import { map } from 'rxjs/operators';
import { Todolist } from '../modal/Todolist';
import { Observable } from 'rxjs';
import { UserService } from '../Services/user.service';
@Component({
selector: 'app-todolist',
templateUrl: './todolist.page.html',
styleUrls: ['./todolist.page.scss'],
})
export class TodolistPage implements OnInit {
minDate = new Date().toISOString();
house;
currentHouse:House;
currentHouseId: string;
DB;
todoArray = [];
todo = {
title: '',
description: '',
last_Date: new Date().toISOString(),
}
constructor(private afs: AngularFirestore,
private router: Router,
private houseService: HouseService,
private userService: UserService,private navCtrl: NavController) {
//get ID of house from URL
let id1 = this.router.url.split('id=');
let id2 = id1[1].toString();
let id3 = id2.split('/');
let id = id3[0].toString();
this.houseService.getHouse(id);
if (id) {
this.houseService.getHouse(id).subscribe(house => {
this.currentHouse = house;
this.house = id;
});}
//initialise DB
this.DB = this.afs.collection('house').doc(id).collection('todolist');
this.DB.snapshotChanges().subscribe(colSnap => {
this.todoArray = [];
colSnap.forEach(snap => {
let todo: any = snap.payload.doc.data();
todo.id = snap.payload.doc.id;
todo.last_Date = new Date(todo.last_Date).toDateString();
this.todoArray.push(todo);
});
});
}//end constructor
ngOnInit() {
}
gotoAddToDo(house:string){
this.navCtrl.navigateForward(['/add-todo',{id:house}]);
}
}
add-todo.ts
import { Component, OnInit } from '@angular/core';
import { NavController } from '@ionic/angular';
import { ToastController } from '@ionic/angular';
import { House, HouseService } from '../Services/house.service';
import { ActivatedRoute, Route, Router } from '@angular/router';
import { TodoserviceService } from '../Services/todoservice.service';
import { AngularFirestore } from '@angular/fire/firestore';
import { map } from 'rxjs/operators';
import { Todolist } from '../modal/Todolist';
import { Observable } from 'rxjs';
import { UserService } from '../Services/user.service';
@Component({
selector: 'app-add-todo',
templateUrl: './add-todo.page.html',
styleUrls: ['./add-todo.page.scss'],
})
export class AddTodoPage implements OnInit {last_Date = new Date().toISOString();
currentHouse;
house;
currentHouseId;
DB;
todoArray = [];
todo = {
title: '',
description: '',
last_Date: new Date().toISOString(),
}
constructor(private afs: AngularFirestore,
private router: Router,
private houseService: HouseService,
private userService: UserService) {
let id1 = this.router.url.split('id=');
let id2 = id1[1].toString();
let id3 = id2.split('/');
let id = id3[0].toString();
this.houseService.getHouse(id);
if (id) {
this.houseService.getHouse(id).subscribe(house => {
this.currentHouse = house;
this.house = id;
});}
//initialise DB
this.DB = this.afs.collection('house').doc(id).collection('todolist');
this.DB.snapshotChanges().subscribe(colSnap => {
this.todoArray = [];
colSnap.forEach(snap => {
let todo: any = snap.payload.doc.data();
todo.id = snap.payload.doc.id;
todo.last_Date = new Date(todo.last_Date).toDateString();
this.todoArray.push(todo);
});
});
}//end constructor
ngOnInit() {
}
addtodo(){
this.DB.add(this.todo);
this.resetTodo();
console.log("todoadded")
}
//fucntion to reset values of bill object
resetTodo(){
this.todo = {
title: '',
description: '',
last_Date: new Date().toISOString(),
}
}
}
Страница Todo с URL-адресом, соответствующим этому параметру c house
addtopage с URL-адресом без определения