Inserting if statement inside ES6 template literal

后端 未结 5 1814
慢半拍i
慢半拍i 2021-02-01 16:15

I have a simple ajax request returning some data and then inserting into a template literal. I was wondering if it it possible to insert an \'if\' statement inside the template?

5条回答
  •  再見小時候
    2021-02-01 16:49

    const heading = 'head';
    const location = 'erode';
    const district = 'erode';
    
    const isSameLocationDistrict = (location === district) || false;
    const storeSlug = `${heading} ${isSameLocationDistrict === true ? location : `${location } ${district}`}`;
    console.log(storeSlug);
    
    // "head erode"
    // "head erode1 erode"
    

提交回复
热议问题