0

매개 변수 값을 정의하지 않으면 사용자 지정 핸들 막대 도우미를 만들었고 다음 오류가 발생합니다.핸들 막대 TypeError : 정의되지 않은 'fn'속성을 읽을 수 없습니다.

module.exports = function(src, color, classatr, options) { 

    if (typeof src === 'undefined') src = ''; 
    if (typeof color === 'undefined') color = ''; 
    if (typeof classatr === 'undefined') classatr = ''; 

    var bg = '<table class="'+ classatr +'" cellpadding="0" cellspacing="0" border="0" width="100%">\ 
     <tr>\ 
     <td background="'+ src +'" bgcolor="'+ color +'" valign="top">\ 
      <!--[if gte mso 9]>\ 
      <v:rect xmlns:v="urn:schemas-microsoft-com:vml" fill="true" stroke="false" style="mso-width-percent:1000;">\ 
      <v:fill type="tile" src="'+ src +'" color="#'+ color +'" />\ 
      <v:textbox style="mso-fit-shape-to-text:true" inset="0,0,0,0">\ 
      <![endif]-->\ 
      <div>' 
      + options.fn(this) + 
      '</div>\ 
      <!--[if gte mso 9]>\ 
      </v:textbox>\ 
      </v:rect>\ 
      <![endif]-->\ 
     </td>\ 
     </tr>\ 
    </table>'; 

    return bg; 
} 

I과 같은 세 가지 매개 변수를 정의 할 경우 작동 :

{{#bg-img 'assets/img/hero-header.jpg' '000000' 'my-class'}} 
    <container> 
     <row> 
      <columns> 
      </columns> 
     </row> 
    </container> 
{{/bg-img}} 

을 내가 매개 변수 콘솔을 정의하지 않는 경우는 "핸들 바 형식 오류를 : 정의의 'FN을'속성을 읽을 수 없습니다"보여줍니다.

{{#bg-img }} 
    <container> 
     <row> 
      <columns> 
      </columns> 
     </row> 
    </container> 
{{/bg-img}} 

내가 뭘 잘못하고 있는지에 대한 아이디어가 있으십니까?

업데이트 : 아래 제안 된 "null"로 확인했지만 여전히 같은 오류입니다.

if (typeof src === 'undefined' || src === null) src = ''; 
if (typeof color === 'undefined' || color === null) color = ''; 
if (typeof classatr === 'undefined' || classatr === null) classatr = ''; 

답변

2

null 매개 변수를 제공하지 않으려는 경우 사용하십시오. 따라서 다음 코드가 작동해야합니다.

{{#bg-img null null null}} 
    <container> 
     <row> 
      <columns> 
      </columns> 
     </row> 
    </container> 
{{/bg-img}} 
+0

"null"을 넣을 필요가없는 방법이 있습니까 –

+0

거기 있다고 생각하지 않습니다. 왜냐하면'null'을 사용함으로써 매개 변수가 존재한다는 것을 (정의 된 것을 의미하는) 함수에 알려주고 있기 때문에 어떤 값도 포함하지 않기 때문입니다. 당신은 다른 무엇으로도 할 수 없습니다 (즉, '정의되지 않음'). 그래서'null'은 실제로 모든 매개 변수를 제공하는 승/O를 해결할 수있는 가장 좋은 방법입니다. – m87

+0

트릭을 수행하는 {{# bg-img '' '' ''}}. 도와 주셔서 감사합니다. 답변으로 표시되었습니다. –

0

속성이 null인지, 정의되지 않았습니까?

+0

null로 지정되고 정의되지 않은 여전히 ​​같은 오류입니다. 여전히 같은 유형 오류가 발생합니다. –

1

함수 서명에 선택적 매개 변수를 선언하면 안됩니다. 핸들 막대는 함수에 전달 된 제공된 옵션 객체에 해시 객체를 넣습니다. 해시 객체를 통해 제공된 모든 매개 변수에 액세스 할 수 있습니다. Block Helpers Documentation의 "해시 인수"섹션을 참조하십시오.

module.exports = function(options) { 

var src = options.hash.src; 
var color = options.hash.color; 
var classatr = options.hash.classatr; 
if (typeof src === 'undefined') src = ''; 
if (typeof color === 'undefined') color = ''; 
if (typeof classatr === 'undefined') classatr = ''; 
...