Vietnam

    Nodejs.vn

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Tags
    • Groups
    • Search

    Load template header footer cho router

    Hỏi Đáp
    0
    3
    889
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      Bảo Quốc Nguyễn last edited by

      Chào các bạn !
      Cho mình hỏi có cách nào mình load header và footer để áp dụng cho các router không ? Vì mình mới học nên có nhiều chỗ chưa hiểu.
      VD:
      router.use(function (req, res, next) {
      var logo = "logo/logo.png";
      }
      router.get('/home',function(req, res, next){
      res.render('home/home_view',{
      title: 'Trang chủ',
      logo: logo
      //logo : 'logo/logo.png'
      });
      }
      Lúc hiển thị thì logo nó ko tìm thấy ảnh. Nếu viết như trong comment thì nó mới tìm thấy
      Thanks các pro.

      1 Reply Last reply Reply Quote 0
      • W
        way.code last edited by

        Bạn nên xem lại kiến thức cơ bản của javascript (variable scope), trong trường hợp này mình có thể sửa lại như sau :

        var logo = "logo/logo.png";
        router.get('/home',function(req, res, next){
                res.render('home/home_view',{
                       title: 'Trang chủ',
                       logo: logo
               });
        }
        

        Hoặc gán biến logo vào request

        router.use(function (req, res, next) {
              req.logo = "logo/logo.png";
        }
        router.get('/home',function(req, res, next){
                res.render('home/home_view',{
                       title: 'Trang chủ',
                       logo: req.logo
               });
        }
        
        B 1 Reply Last reply Reply Quote 0
        • B
          Bảo Quốc Nguyễn @way.code last edited by

          @way.code Thanks !

          1 Reply Last reply Reply Quote 0
          • First post
            Last post