使用Angular 2在n秒后自动重定向

时间:2017-02-27 14:21:41

标签: angular angular2-routing

我想显示“n”秒的页面,然后重定向到另一条路线。

遇到几个有关Angular 1.x中“n”秒后自动重定向的stackoverflow帖子(url1url2)。但我很困惑如何在Angular2中实现相同的功能?

2 个答案:

答案 0 :(得分:16)

您可以从Router注入并使用@angular/router并导航setTimeout

import { Router } from '@angular/router';

constructor(private router: Router) {}

ngOnInit() {
    // do init at here for current route.

    setTimeout(() => {
        this.router.navigate(['nextRoute']);
    }, 5000);  //5s
}

答案 1 :(得分:0)

有点粗略,但这会起作用。

exports.createProduct = (req, res) => {
  let form = new formidable.IncomingForm();
  form.keepExtensions = true;
  form.multiles = true;
  form.parse(req, (err, fields, files) => {
    if (err) {
      return res.status(400).json({
        error: "Problem with image",
      });
    }
    //destructure the fields
    const { name, description, price, category, stock } = fields;
    if (!name || !description || !price || !category || !stock) {
      return res.status(400).json({
        error: "All fields are required!",
      });
    }
    fields.category = category.split(",");
    fields.subcategory = fields.subcategory.split(",");
    //TODO : rescrition on fields
    let product = new Product(fields);

    console.log(files); //i get empty object {}

    //handle file here
    //everything below is comment dont look

  });
};