Unable to connect to 'highlight' as it is not a recognized attribute of 'code'

I have been experimenting with ngx-highlightjs and encountered an issue while trying to implement it in one of my module files. Due to having multiple modules, I am importing the HighlightModule only in the specific module where highlighting functionality is required, rather than in the App Module.

import { HighlightModule, HIGHLIGHT_OPTIONS, HighlightOptions} from 'ngx-highlightjs';

@NgModule({
  declarations: [
    EnvironmentComponent,
    RepositoryComponent
  ],
  imports: [
    CommonModule,
    SharedModule,
    HighlightModule
  ],
  providers: [
    EnvironmentService,
    AuthService,
    {
      provide: HIGHLIGHT_OPTIONS,
      useValue: {
        coreLibraryLoader: () => import('highlight.js/lib/core'),
        lineNumbersLoader: () => import('highlightjs-line-numbers.js'),
        languages: {
          typescript: () => import('highlight.js/lib/languages/typescript'),
          css: () => import('highlight.js/lib/languages/css'),
          xml: () => import('highlight.js/lib/languages/xml')
        }
      }
    }
  ],
})

In the component's template, the code structure is as follows:

<pre><code [highlight]="fileData" (highlighted)="onHighlight($event)"></code></pre>

However, upon implementation, I encountered the error message "Can't bind to 'highlight' since it isn't a known property of 'code'" and the output is not displaying. How can I resolve this issue?

Answer №1

It seems that the problem is related to Angular not recognizing the native JavaScript library, highlight.js. This is likely because the highlight attribute is a directive defined by highlight.js.

I encountered the same issue and was able to resolve it by taking the following steps:

1- I manually installed highlight.js in my project directory.

npm install highlight.js

2- I added the following lines to the scripts and styles sections in my angular.json file.

    "styles":[ 
     "./node_modules/highlight.js/styles/default.css"
     ],
    "scripts": [
     "./node_modules/highlight.js/lib/highlight.js"
    ]

Answer №2

Consider enhancing your component by including the HighlightModule.

Add the following to your myCompo.module.ts file:

import { HighlightModule } from 'ngx-highlightjs';    
imports: [
    ....
    HighlightModule,

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtain gridview row coordinates to create a canvas drawing using Raphael.js

I need to create various shapes based on the status of each row. In order to make it compatible with Internet Explorer, I have integrated the Raphael JavaScript library into my project. The backend is built using ASP.NET and C#. To draw these shapes, I mus ...

Is Angular's forkJoin being phased out?

Upon opening my Angular project today, I came across a warning message that said: The utilization of forkJoin is marked as deprecated: resultSelector is deprecated, it is recommended to use pipe to map instead (deprecation) https://i.sstatic.net/vFpeu.pn ...

What causes a 500 error when using PHP eval in conjunction with AJAX?

Working within a system where all PHP code resides in a database for dynamic alterations has presented me with an interesting challenge. While the code displays perfectly on the page, calling the same code via AJAX triggers a frustrating error 500. I' ...

Creating two variables that share an identical name

Can variables with the same name set outside of a function be called within the function? var a = $(window).width(); // This is the variable I want to call if(!$.isFunction(p)){ var a = $(window).height(); // Not this one alert(a); } FIDDLE ...

Error: The function 'document.getsElementsByClassName()' is not defined when evaluating 'undefined'

Actual Error TypeError: undefined is not a function (evaluating 'ActiveElem[i].hasClass('active'); HTML <div class = 'Carousel-Inner'> <div class="morningSlide active"> <img src="/Users/KO527/Sites/TarasDeli ...

Encountering Syntax Errors during Angular 6 production build

I encountered an issue with my project. Everything was running smoothly, and even when I executed the command ng build --prod, it compiled successfully and generated the dist folder in my project directory. However, after copying this folder to the htdoc ...

Ways to resolve discrepancies between Bootstrap-3 and Bootstrap-4 versions

I am currently learning Angular through an older tutorial that uses Bootstrap 3.3.7. However, I have downloaded the latest version of Bootstrap, 4.4.1, which is causing some issues as I cannot follow along with the instructor and utilize certain features l ...

Is it possible to simultaneously run multiple functions with event listeners on a canvas?

I'm attempting to create a canvas function that displays the real-time mouse cursor location within the canvas and, upon clicking, should draw a circle. I came across this code snippet that reveals the x and y coordinates of the mouse: document.addEve ...

Detecting collisions using CSS animation

I'm currently working on a unique "game" project. Check out the code snippet here: jsfiddle function update() { coyote.applyForce(gravity); coyote.edges(); coyote.update(); cactus.update(); if (coyote.intersects(cactus)){ alert("colisio ...

What is the significance of enclosing Button within CardActions in Material-UI?

As I explored the Card documentation on MUI, I found that the Button inside the card is always enclosed within CardActions. However, I couldn't quite grasp the purpose of this tag as it wasn't clearly explained in the documentation. The only noti ...

Shift a div out of view (using margin: 0px auto)

How can I achieve a sliding effect for two horizontally centered divs, #box1 and #box2, when a button with the class .class is clicked? The desired animation involves moving #box1 to the left and off the screen, while #box2 slides into view from the right. ...

JavaScript allows you to export a variable from a function

I simply want to access the value of the userId variable for any use. async function getMyData(){ const token = ''; spotifyApi.setAccessToken(token); const data = await spotifyApi.getMe(); let userId = data.body.id; retu ...

Undo changes in Sequelize transaction using a specific rollback target

In my current project, I am utilizing managed transactions. There is a specific scenario where I need to implement a single transaction and revert to a certain savepoint if it encounters an error, but still ensure that the changes are committed in the end. ...

Issue - Following error occurred in the file connection.js located in node_modules/mysql2 library: Module not found: Unable to locate 'tls' module

I've encountered an error in our Next JS applications where tls is not found. I have tried several troubleshooting steps including: 1. Updating Node modules 2. Using both mysql and mysql2 3. Running npm cache clean --force 4. Removing node_modules di ...

Sending information from one page to another and then sending it once more

I am currently utilizing the following code to submit a form's data to : <script type="text/javascript"> $(document).ready(function(){ $("#textnextofkin").validate({ debug: false, rules: { ...

Are you on the lookout for an Angular2 visual form editor or a robust form engine that allows you to effortlessly create forms using a GUI, generator, or centralized configuration

In our development team, we are currently diving into several Angular2< projects. While my colleagues are comfortable coding large forms directly with Typescript and HTML in our Angular 2< projects, I am not completely satisfied with this method. We ...

Using TypeScript with AngularJS: A directive that returns a value of 0 when using scope.$eval

I decided to create a custom directive that would compile HTML and bind it to my element. Here is the code for my directive: export class BindCompileHtmlDirective implements ng.IDirective { restrict = 'A'; link = (scope: ng. ...

The challenges of dealing with duplicate identifiers caused by nesting npm packages in TypeScript

I am facing an issue with my project structure where I have a node_modules folder at the root level and another one within a subfolder named functions. The directory layout looks like this, ├── functions │   ├── index.js │   ├── ...

Navigate to the top of the page using jQuery

Attempting to implement a simple "scroll jump to target" functionality. I've managed to set it up for all parts except the "scroll to top". The jumping works based on the tag's id, so it navigates well everywhere else, but not to the very top. He ...

Integrating MongoDB data values with Node.js for enhanced functionality

Hey everyone, I'm looking to add two field values {type:Number} from a MongoDB collection using node js and then store the result back in the same collection. To achieve this, I have outlined the steps below: Retrieve the data value from MongoDB wit ...